【发布时间】:2020-01-13 16:17:35
【问题描述】:
目前我已经设置了类似的路线
const routes: Routes = [
{path: "", redirectTo: "route1", pathMatch: "full"},
{path: "route1", children: [
{path: "", redirectTo: "subRoute1", pathMatch: "full"},
{path: "subRoute1", component: SubRouteComponent}
]}
]
仅访问 {domain} 即可重定向到 {domain}/route1/subRoute1。
然后在我的组件中我有类似
<a [routerLink]="['/route1/subRoute1']">Link</a>
==============================
我的问题:
==============================
我想为整个应用程序添加一个新的根 url 路径,该路径应用于所有路由,但我不想将所有 [routerLink] 实例更改为指向这条新路径。
例如,我想像这样更改路由,其中 site1 是新的根 url 路径:
const routes: Routes = [
{path: "", redirectTo: "site1", pathMatch: "full"},
{path: "site1", children: [
{path: "", redirectTo: "route1", pathMatch: "full"},
{path: "route1", children: [
{path: "", redirectTo: "subRoute1", pathMatch: "full"},
{path: "subRoute1", component: SubRouteComponent}
]}
]}
]
然后我必须将 routerLinks 更改为
<a [routerLink]="['/site1/route1/subRoute1']">Link</a>
据我所知,我必须更改代码中的所有 [routerLink] 实例以反映这个新的 url 路径更改,我要避免这样做。
除了需要经常更改根路径的能力之外,我可能有 100 个 [routerLink] 引用。
我现在正在尝试的解决方案是检测“NavigationStart”路由调用并在该点修改路径以添加新的根路径,但我认为它不会像我希望的那样工作。
另外,这是另一种处理方式:https://angular.io/guide/router#changing-heroes-to-superheroes
但我想避免臃肿的路由配置。
【问题讨论】:
标签: angular angular2-routing routerlink