我无法从我的 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 的值。目前相同的代码给我 null。
最佳答案
原来我需要以下内容:
this.route.parent.parent.parent.snapshot.paramMap.get('id')
感谢@jonrsharpe 为我指明了正确的方向
关于angular - 在 Angular 子组件中获取 Route 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51453721/