Use something like this:
export const routes: Routes = [
//...
{ path: 'colors/:color', component: ColorsComponent },
//...
];
You can use these routes like this in your template:
<a [routerLink]="['/colors', 'red']">Something</a>
Or:
goToRed() {
this.router.navigate(['/colors', 'red']);
}
Of course 'red'
can be a variable too.
And you can subscribe to this parameter in your component's ngOnInit()
:
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.color = params['color'];
//You can check the color here.
});
}
You can use this method for the Cars and Foods too.
UPDATE:
If you want to pass aditional 'parameters'. You can try this:
{
path: 'colors/:color',
component: ColorComponent,
data: {someValue: 123}
}
In your component, you can use this.route.data
to access it. The URL will still be look like this: localhost:4200/colors/red
for example.