如何区分产品页面和类别页面?

时间:2018-01-31 08:58:01

标签: html angularjs angular routing

所以我正在开展一个小项目来建立一个网店。 但我真的很想解决这部分路由问题。

这是我的路由代码:

 { path: 'productrange', component: ProductRangeComponent },
 { path: 'productrange/:productgroup', component: ProductRangeComponent },
 { path: 'productrange/:productgroup/:producttype', component: ProductRangeComponent },
 { path: 'products/:productgroup/:producttype', component: ProductsComponent },
 { path: 'products/:productgroup/:producttype/:productsubtype', component: ProductsComponent },

所以这可行,但我认为这不是最好的方法。因为我使用两个不同的组件和两个不同的路径。

这就是我希望它:

 { path: 'products', component: ProductsComponent },
 { path: 'products/:productgroup', component: ProductsComponent },
 { path: 'products/:productgroup/:producttype', component: ProductsComponent },
 { path: 'products/:productgroup/:producttype/:productsubtype', component: ProductsComponent },

只要产品只在最后一个路径中显示,这就可以工作,但情况并非总是如此。有时候第三条路径甚至第二条路径都可能有产品。但我不知道如何让系统知道应该有产品展示。

当然,我可以对数据库进行多次调用,检查是否有更多类别,如果没有搜索产品并从那里开始。但我不相信这是正确的方法,因为这意味着不必要的服务器和数据库调用。

谢谢!

1 个答案:

答案 0 :(得分:0)

添加子路线。

{ path: 'products', component: ProductsComponent, children: [
   { path: ':productgroup', component: ProductRangeComponent }
  ]
}

另见https://angular-2-training-book.rangle.io/handout/routing/child_routes.html