在Angular子组件中获取Route参数

Rob*_*lls 3 angular

我无法从Angular 6应用程序的子组件中获取路由参数

我在app-routing.module中设置了以下路由:

{
    path: 'item/:id',
    component: ItemLayoutComponent,
    children: [
        { path: '', redirectTo: 'inbox',  pathMatch: 'full' },
        { path: 'inbox', loadChildren: './feature/inbox/inbox.module#InboxModule' }
    ]
},
Run Code Online (Sandbox Code Playgroud)

然后在我的inbox-routing-module.ts中:

{
    path: '',
    component: InboxComponent,
    children: [
        { path: '', redirectTo: 'messages',  pathMatch: 'full' },
        {
            path: 'messages', component: MessagesComponent
        },
    ]
}
Run Code Online (Sandbox Code Playgroud)

这使我可以使用一个类似于以下网址的URL到达我的消息组件:

item/5200/inbox/messages
Run Code Online (Sandbox Code Playgroud)

我的问题是,在我的ItemLayoutComponent中,我可以使用以下代码获取id字段的值(在本例中为5200):

this.route.snapshot.paramMap.get('id')
Run Code Online (Sandbox Code Playgroud)

但是,我需要在MessagesComponent中使用此ID的值。目前,相同的代码使我为空。

Rob*_*lls 6

原来我需要以下内容:

this.route.parent.parent.parent.snapshot.paramMap.get('id')
Run Code Online (Sandbox Code Playgroud)

感谢@jonrsharpe为我指出正确的方向