角度路由器 - 如何查找到父组件的路由

问题描述 投票:0回答:1

想象一下以这种方式设置的路线:

module1Routes=  [
       {path: 'parent1': component: ParentComponent : children:[
           {path: 'child': component: ChildComponent},
           {path: 'result': component: ResultComponent}
       ]}
]

module2Routes=  [
       {path: 'parent2': component: Parent2Component : children:[
           {path: 'child': component: ChildComponent},
           {path: 'result': component: ResultComponent}
       ]}
]

现在的情况是这样的。我有2个模块,在处理ChildComponent结束时,我想去ResultComponent

问题是如何知道qazxsw poi的正确路径是什么?

如果用户在路径ResultComponent我想下一步是parent1/child,但当用户在parent1/result下一步应该是parent2/child

在我的parent2/result我有方法ChildComponent,其责任是导航到goToResult()。我很困惑如何知道***/result中的内容?

所以问题是如何知道(在ChildComponent中)这个孩子的父母的真正路线是什么?

我完全陷入了困境。我试图建立没有子片段的父路径,但这不起作用。有任何想法吗?

我知道我可以在***的配置中使用relativeTo关键字。问题是我想在某些服务中调用router.navigate(),所以我需要传递一个导航路径

angular angular2-routing angular-routing
1个回答
0
投票

您可以将路由器添加到组件中,并从当前URL中删除子字符串。

变体A)路由器+字符串操作(component.ts)

router.navigate()

如果子项可能有多次出现,或者您需要删除查询参数,则可以调整替换函数。

变体B)ActivatedRoute(component.ts)

constructor(private router: Router, private yourService: YourService) {

}

goToResult() {
   const path = this.router.url.replace('/child', '');
   this.yourService.goTo(path);
}
© www.soinside.com 2019 - 2024. All rights reserved.