如何处理angular5个递归的未知确切数目路由器参数?
How to handle angular 5 recursive unknown exact number router parameters?
有没有办法处理递归未知的路由器参数的确切数量?
例如:
我们有产品类别,产品类别可以有子类别,子类别可以有自己的子类别等等。主要有几个条件:
- 如果此类类别没有子类别,我们将重定向到
/categories/{id}/items
,这将打开项目列表组件。
- 如果类别有子类别,它应该被重定向到下一个嵌套树级别
/categories/{id}/{id}/.../{id}
,它应该打开最后一个 categoryId 子类别列表组件。
- 到达没有子类别的最后一个类别后,将显示项目列表组件
/categories/{id}/{id}/.../{id}/items
。
检查和重定向的解决方案是使用路由器解析器。但是如何在路由模块中跟踪这些 url?
从我的角度来看,路线应该是这样的:
{
path: '/categories/:id',
component: SubcategoriesListComponent
},
{
path: '/categories/:id/**/:id',
component: SubcategoriesListComponent,
},
{
path: '/categories/:id/**/:id/items',
component: CategoryItemsListComponent
}
是否可以这样实现?
无组件路由的可能解决方案:
在路由配置中
{
path: 'categories/:id', children: [
{path: '**', component: SubcategoriesListComponent}
]
}
在组件文件中:
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
});
}
}
这是 output example(我在我的项目 ErrorComponent 上测试过所以不要混淆)
有没有办法处理递归未知的路由器参数的确切数量?
例如:
我们有产品类别,产品类别可以有子类别,子类别可以有自己的子类别等等。主要有几个条件:
- 如果此类类别没有子类别,我们将重定向到
/categories/{id}/items
,这将打开项目列表组件。 - 如果类别有子类别,它应该被重定向到下一个嵌套树级别
/categories/{id}/{id}/.../{id}
,它应该打开最后一个 categoryId 子类别列表组件。 - 到达没有子类别的最后一个类别后,将显示项目列表组件
/categories/{id}/{id}/.../{id}/items
。
检查和重定向的解决方案是使用路由器解析器。但是如何在路由模块中跟踪这些 url?
从我的角度来看,路线应该是这样的:
{
path: '/categories/:id',
component: SubcategoriesListComponent
},
{
path: '/categories/:id/**/:id',
component: SubcategoriesListComponent,
},
{
path: '/categories/:id/**/:id/items',
component: CategoryItemsListComponent
}
是否可以这样实现?
无组件路由的可能解决方案: 在路由配置中
{
path: 'categories/:id', children: [
{path: '**', component: SubcategoriesListComponent}
]
}
在组件文件中:
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
});
}
}
这是 output example(我在我的项目 ErrorComponent 上测试过所以不要混淆)