angular - 在 Angular 子组件中获取 Route 参数

标签 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' }
    ]
},

然后在我的 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/

相关文章:

javascript - 仅在 Safari 上输入密码类型会阻止退格键

javascript - 覆盖 app.get ('*' , func) 中的路由

javascript - 无法读取 undefined 的属性...,在 Object.callback 从 Native In angular 2

javascript - Angular4 - 如何检查 Twitter 媒体 key 是否存在或未定义

angular - 如何以编程方式打开垫菜单?

javascript - 如何避免 Angular 服务中的重复方法

Angular 6/7 AOT : Dynamic template render - load JitCompiler for module

Angular 7 生产构建失败

angular - 使用 Ionic3 时出现“不支持”错误

javascript - CSS 无法按 Angular 插值工作