【发布时间】:2018-02-04 17:51:59
【问题描述】:
我试图在延迟加载的路由组件中获取父路由参数,但是使用activatedRoute.parent.params 似乎不起作用?我有一个确实有效的 sn-p,但是它涉及使用“魔术”数组索引号来检索值....
为什么会出现这种情况?是否有一个 cleaner 方法可以让我不必检索数组值 (this.activatedRoute.pathFromRoot[1])?
我有以下延迟加载模块的路线:
parent.route
routes:Routes = [
{
path: "dashboard/:id",
component: dashboard.DashboardComponent,
canActivate: [guard.LoggedInGuard],
children: [
{
path: "my-dock",
loadChildren: 'path/child.module#Child'
}
]
}
];
然后在child.module 的默认组件中我有以下内容:
child.component
ngOnInit() {
this.activatedRoute.pathFromRoot[1].params.subscribe((params: Params) => {
console.log(params); // returns {id: "123456789"}
});
this.activatedRoute.parent.params.subscribe((params: Params) => {
console.log(params); // returns {}
});
}
为什么我不能使用this.activatedRoute.parent.params 来获取id 参数?
【问题讨论】:
-
您能添加您的子模块路由的样子吗?看来您可能正在寻找直系父母,您需要使用
this.activatedRoute.parent.parent来使用祖父母,干杯!! -
@MadhuRanjan 在网址中?
-
不,路由配置,就像你为 parent.route 共享的一样。
-
@MadhuRanjan 啊哈是的
-
啊是的,因为您要添加子路由或者您正在寻找错误的父路由? :)
标签: angular typescript