Sam Willis
Sam Willis

Reputation: 4211

Angular 8 route params in route data

I have the following route:

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

In the breadcrumbs array I would like to populate the link property using the route param :introducerId but have been unable to do so. Is there a way to do this?

Upvotes: 0

Views: 881

Answers (1)

Daniel Twigg
Daniel Twigg

Reputation: 759

I am not 100% sure if this would work but you could try something

{
    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: '' },
            ]
        }
    },
}

And then add the method getIntroducerId to your component, and make your usage of data.breadcrumbs call it as a function instead of just using the object property.

Upvotes: 2

Related Questions