作者:小编106 | 来源:互联网 | 2022-12-10 02:31
我无法从Angular 6应用程序的子组件中获取路由参数
我在app-routing.module中设置了以下路由:
{
path: 'item/:id',
component: ItemLayoutComponent,
children: [
{ path: '', redirectTo: 'inbox', pathMatch: 'full' },
{ path: 'inbox', loadChildren: './feature/inbox/inbox.module#InboxModule' }
]
},
然后在我的inbox-routing-module.ts中:
{
path: '',
component: InboxComponent,
children: [
{ path: '', redirectTo: 'messages', pathMatch: 'full' },
{
path: 'messages', component: MessagesComponent
},
]
}
这使我可以使用一个类似于以下网址的URL到达我的消息组件:
item/5200/inbox/messages
我的问题是,在我的ItemLayoutComponent中,我可以使用以下代码获取id字段的值(在本例中为5200):
this.route.snapshot.paramMap.get('id')
但是,我需要在MessagesComponent中使用此ID的值。目前,相同的代码使我为空。
1> Robbie Mills..:
原来我需要以下内容:
this.route.parent.parent.parent.snapshot.paramMap.get('id')
感谢@jonrsharpe为我指出正确的方向