Angular5路由器如何匹配任意深度的路径?

How to match an arbitrarily deep path with Angular 5 router?

在我的应用程序中,每个用户都可以访问类别和子类别树中的一堆 'items'。这棵树可以是任意深度并且是动态生成的。我可以查询它,但我想避免这样做,因为我只想在基于 url 的路由器插座中显示 'ItemComponent',如下所示:

localhost:4200/items/cat_a/sub_a/item_x
localhost:4200/items/cat_a/sub_a/sub_sub_a/item_y
localhost:4200/items/cat_b/item_z

最好我想通过匹配以 "items/" 开头的任何路径并自己在 'ItemComponent' 中解析其余路径来做到这一点,但我能够让它工作的唯一方法是这样的:

{ path: 'items/:a', component: ItemComponent }
{ path: 'items/:a/:b', component: ItemComponent }
{ path: 'items/:a/:b/:c', component: ItemComponent }
{ path: 'items/:a/:b/:c/:d', component: ItemComponent }
{ path: 'items/:a/:b/:c/:d/:e', component: ItemComponent }
...
...

然而,这似乎有点愚蠢,而且如果有一条路径比我选择在这里停留的地方只深一层,那么我显然有麻烦了。我该如何改进?

在您的路由定义中使用 matcher(而不是路径),并决定该路由应匹配哪些 URL。在您的情况下,它将匹配任何以 'items'.

开头的 URL