使用角度2路由器使基本URL不敏感

时间:2016-07-27 12:21:29

标签: angular

我的应用程序未部署在我们网站的根目录下,它位于site.com/TRAP_beta,并且组件路由定义为此{ path: '', component: TrapFormComponent }。我将基本网址标记设置为<base href="/TRAP_beta/">

当我导航到site.com/TRAP_beta时,一切都与世界相关并且应用程序按预期工作,但如果我导航到site.com/trap_beta或其他任何其他类型的网址,则路由器无法找到路径它打破了。

如何让网址对角度2不敏感?

2 个答案:

答案 0 :(得分:1)

我最终在脚本标记中动态制作基本元素,该标记在其他任何内容加载之前运行。

    var path = location.pathname.substr(1).match(/[a-z_0-1]*/i)[0];
    var base = document.createElement('base');
    base.href = '/' + location.pathname.substr(1).match(/[a-z_0-1]*/i)[0] + '/';
    document.head.appendChild(base);

这似乎解决了我的问题,并允许我继续使用router.navigate而不是navigateByUrl

答案 1 :(得分:0)

尝试使用router.navigateByUrl('/'+'nextValue');代替router.navigate(['nextValue']);。因为指令将基于提供的网址获取。

相关问题