Is there a way to handle recursively unknown exact number of router parameters?
For example:
We have products categories, which can have subcategories, subcategories can have it's own subcategories and so on. There are a few main conditions:
/categories/{id}/items
that will open items list component./categories/{id}/{id}/.../{id}
which should open the last categoryId subcategories list component./categories/{id}/{id}/.../{id}/items
.The solutions to check and redirect is to have router resolver. But how track those urls in routing module ?
From my perspective the routes should look something like this:
{
path: '/categories/:id',
component: SubcategoriesListComponent
},
{
path: '/categories/:id/**/:id',
component: SubcategoriesListComponent,
},
{
path: '/categories/:id/**/:id/items',
component: CategoryItemsListComponent
}
Is it possible implement it in a such way ?
The first way is through the route snapshot. The route snapshot provides the initial value of the route parameter map (called the paramMap ). You can access the parameters directly without subscribing or adding observable operators. The paramMap provides methods to handle parameter access like get , getAll , and has .
Possible solution with componentless routes: in routes config
{
path: 'categories/:id', children: [
{path: '**', component: SubcategoriesListComponent}
]
}
in component file:
export class SubcategoriesListComponent {
constructor(aroute: ActivatedRoute) {
aroute.url.subscribe((data) => {
console.log('params ', data); //contains all the segments so put logic here of determining what to do according to nesting depth
});
}
}
Here is the output example (i tested on my project ErrorComponent so don't be confused)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With