我的应用程序中有一个没有路由模块的核心模块和一个带有路由模块的功能模块。在我的功能模块的组件中,我可以通过在构造函数中使用它来访问路由参数:
this.route.params.subscribe((params: Params) => {
console.log("params['searchTerm']");
console.log(params['searchTerm']);
this.searchTerm = params['searchTerm'];
});
现在,在我的核心模块的组件中,我也想使用上面的代码来访问参数。由于路由是在功能模块路由模块中定义的,当我尝试访问参数时,我只是得到了未定义的信息。
我怎样才能访问参数?
最佳答案
这是可能的解决方案之一:
constructor(private route: ActivatedRoute, private router: Router) {
router.events.subscribe((e) => {
let currentRoute = this.route.root;
while (currentRoute.children[0] !== undefined) {
currentRoute = currentRoute.children[0];
}
if (e instanceof NavigationEnd) {
// you can do it for exact phase
console.log(currentRoute.snapshot.params);
}
});
}
关于angular - 从另一个模块访问路由参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42692359/