首页 > 解决方案 > 路线数据中的 Angular 8 路线参数

问题描述

我有以下路线:

{
path: 'introducer/:introducerId/branches/create',
component: IntroducerBranchesCreateComponent,
data: {
  pageTitle: 'Add branch',
  breadcrumbs: [
    { title: 'Branches', link: '/introducer/' + :introducerId + '/branches' },
    { title: 'Add', link: '' },
  ],
},
},

在面包屑数组中,我想使用路由参数填充链接属性,:introducerId但无法这样做。有没有办法做到这一点?

标签: angulartypescriptangular-routingangular8

解决方案


我不是 100% 确定这是否可行,但你可以尝试一下

{
    path: 'introducer/:introducerId/branches/create',
    component: IntroducerBranchesCreateComponent,
    data: {
        pageTitle: 'Add branch',
        breadcrumbs: () => {
            const id = this.getIntroducerId();

            return [
                { title: 'Branches', link: `/introducer/${id}/branches` },
                { title: 'Add', link: '' },
            ]
        }
    },
}

然后将该方法添加getIntroducerId到您的组件中,并使用data.breadcrumbs将其作为函数调用而不是仅使用对象属性。


推荐阅读