【问题标题】:How to match an arbitrarily deep path with Angular 5 router?如何将任意深度的路径与 Angular 5 路由器匹配?
【发布时间】:2018-01-24 15:59:31
【问题描述】:

在我的应用中,每个用户都可以访问类别和子类别树中的一堆“项目”。这棵树可以是任何深度并且是动态生成的。我可以查询它,但我想避免这样做,因为我只想根据这样的 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 }
...
...

然而,这似乎有点愚蠢,如果有一条路径比我选择在这里停下来的地方更深一层,我显然就有麻烦了。我该如何改进呢?

【问题讨论】:

    标签: angular angular-routing angular5 angular-router


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多