Sameer K
Sameer K

Reputation: 799

Angular 2 routes load different component based on type of parameter

I need to load different components based on the type of route parameter. if the route parameters is string then load one component like below,

{ path: 'care/action/:string',component: StringComponent},

if the route parameters is number then load another component like below

{ path: 'care/action/:number', component: NumberComponent},

Please note that URL will remain same, only type of parameter will change.

How to achieve this?

Thanks in advance.

Upvotes: 5

Views: 1871

Answers (1)

tlt
tlt

Reputation: 15291

You might want to leverage matcher property on your route configs and pass a matching function to it. Your matcher function would use regular expression to check if its a string (for string route) or number (for number).

Upvotes: 1

Related Questions