【发布时间】:2017-01-02 03:50:36
【问题描述】:
在 Angular 2 rc5 中使用新的路由器并且有一个公共路由,我不确定如何编写。
我需要一个类似这样的网址:
http://localhost:3000/store/{category}/{product-name}
这似乎很简单,直到我补充说我正在寻找可能有多个子类别,例如(我们不知道可能有多少):
http://localhost:3000/store/{category 1}/{sub category 2}/{sub category 3}..../{product-name}
如何创建与子类别和产品匹配的路径。除了“完整”之外,它是否以某种方式使用 pathMatch?
标准 app.routing.ts
const appRoutes: Routes = [
{
path: '/store/:cat',
component: CategoryComponent
},
{
path: '/store/:cat/:product',
component: ProductComponent
}
];
【问题讨论】:
-
请参阅下面的@Godfather 解决方案。让我印象深刻的是应用程序组件的路由器出口,然后是辅助组件中的第二个路由器出口,允许提供和收集子路由。然后使用 ActivatedRoutes,您可以确定更多数据,例如 urlSegments,并使用您喜欢的方法来实现此解决方案。