假设我有两条路线:
{ path: '/path1', component: component1 }
{ path: '/path2', component: component2 }
我在'/ path1'并导航到'/ path2'。
所以我的问题是如何在组件2构造函数或代码中的任何位置获取路由url'/ path1'而不通过任何服务使用resolve或global变量。
Component2.ts
export class Component2{
constructor(router: Router) {
router.events.subscribe((event) => {
// here I want to get route url '/path1' from component1
});
}
}
如果有人需要更多澄清,请告诉我。
我不确定你想怎么用它。但是如果你知道应用程序中的可用路径,你可以简单地在component2中使用:this.router.navigate([“/ path1”]);你可以使用的基本URL,例如:location.host所以你的网址是location.host +“/ path2”这就是你要找的?