【问题标题】:Angular 9 routing anonymous parametersAngular 9 路由匿名参数
【发布时间】:2020-06-01 21:40:24
【问题描述】:

我正在使用 Angular 9,并尝试在 URL 中捕获具有未知数量参数的路由以表达嵌套文件夹结构。

示例组如下所示:/group/:groupId/child1/childofchild1/childofchildofchild1

我不知道 :groupId 参数后面会有多少个参数。

现在,我在 app.module.ts 中有这样的路由

RouterModule.forRoot([
      { path: 'group/:groupId', component: GroupComponent, pathMatch: 'prefix' },
]);

这将匹配 /group/1 之类的 URL,但不会匹配 /group/1/a,并抛出错误消息:“Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'group /1/a'" 尽管是前缀 pathMatch。

我真的很喜欢这样设置路线:

{ path: 'group/:groupId/:path }

并让路由 'group/1/a/b/c' 将 :path 参数解析为 'a/b/c'。

【问题讨论】:

  • 我建议使用 queryParams 而不是路径变量。

标签: angular routes


【解决方案1】:

只需使用通配符,特别是**:

path: 'group/:groupId/**', component: GroupComponent,  //...

【讨论】:

  • 我只是再次尝试以确保。它不起作用。
猜你喜欢
  • 1970-01-01
  • 2020-12-07
  • 2020-07-30
  • 1970-01-01
  • 1970-01-01
  • 2018-10-06
  • 1970-01-01
  • 1970-01-01
  • 2018-04-23
相关资源
最近更新 更多