Angular 角度更改路线参数而不更改子路线
以下是路由器配置:Angular 角度更改路线参数而不更改子路线,angular,Angular,以下是路由器配置: export const routes: Routes = [ { path: 'atc/:atcId', component: AtcComponent, resolve: { atcList: 'atcList', }, children: [ {path: '', redirectTo: 'goal', pathMatch: 'full'}, { path: 'goal',
export const routes: Routes = [
{
path: 'atc/:atcId',
component: AtcComponent,
resolve: {
atcList: 'atcList',
},
children: [
{path: '', redirectTo: 'goal', pathMatch: 'full'},
{
path: 'goal',
component: GoalComponent
},
{
path: 'visit',
component: VisitComponent
},
]
}
];
我想导航到“兄弟”页面。所以,如果我在/atc/1/goal中,它想转到/atc/2/goal。如果我在/atc/1/visit中,我想转到/atc/2/visit
假设我当前的路线是/atc/1/visit
我想导航到id为2的atc
我在一个组件里面。对于这个父组件POV,我的当前路线是/atc/1。它对“目标”或“访问”一无所知。
我是这样做的:
this.router.navigate(['../', '2'], {relativeTo: this.route});
但当我这样做时,它会将其解析为/atc/2。它删除了“访问”或“目标”部分。由于路线原因,它会被重定向到/atc/2/goal
我是否错过了API中的某些内容,或者是否有一种不太老套的方式让孩子们保持原样
多谢各位
编辑:这里有一个stackblitz可以更好地解释这种情况:您应该以这种方式重定向:
this.router.navigate(['../', '2', 'visit'], {relativeTo: this.route});
您应该以这种方式重定向:
this.router.navigate(['../', '2', 'visit'], {relativeTo: this.route});
最简单的答案是肯定的,但我不能静态地放置“visit”,因为我不知道从AtcComponent的角度看我是在/atc/X/visit还是/atc/X/goal中。我将编辑我的问题尝试以下操作:this.router.navigate(['../','2',this.activatedRoute.url[2].path],{relativeTo:this.route});我不确定url的索引您可以再试一次请检查,这是什么。activatedRoute.url[2]。路径变量,可能索引错误,我不确定。我在调试中检查过,它不存在为了确保,您在构造函数内注入了“private activatedRouter:activatedRouter”?最简单的回答是,但是我不能静态地放置“visit”,因为我不知道从AtcComponent的角度看我是在/atc/X/visit还是/atc/X/goal中。我将编辑我的问题尝试以下操作:this.router.navigate(['../','2',this.activatedRoute.url[2].path],{relativeTo:this.route});我不确定url的索引您可以再试一次请检查,这是什么。activatedRoute.url[2]。路径变量,可能索引错误,我不确定。我在调试中检查过,它不存在。要确定,您在构造函数“private activatedRoute:activatedRouter”中注入了什么?