【问题标题】:Angular route with Query Parameter and Appended Route带有查询参数和附加路由的角路由
【发布时间】:2020-06-11 23:08:29
【问题描述】:

我正在使用 Angular 9 应用程序。

我有一个包含以下代码的路由文件:

{ path: 'a/:id', component: AComponent },
{ path: 'a/b', component: BComponent },
{ path: 'a/c', component: CComponent }

我想拥有相同的路由 'a' 和查询参数 id 以及附加的路由 (b & c)

当我尝试导航到 'a/b''a/c' 时,浏览器中的 URL 会更新,但是它实际上并没有导航到所需的路线。

有什么办法可以做到吗?

【问题讨论】:

  • Route a/:id 匹配所有情况。如果你真的需要这种路线,试着改变元素的顺序。最通用的规则应该放在最后。
  • 在 vueJs 中这很常见,你需要 watch 路由,检查一下 stackoverflow.com/questions/34444822/…
  • @KarolTrybulec 是的,你是对的......谢谢!

标签: angular parameters routes query-parameters


【解决方案1】:

路由的顺序很重要,因为Router在匹配路由时使用先匹配获胜的策略,所以更具体的路由应该放在不太具体的路由之上。

在你的情况下:

{ path: 'a/b', component: BComponent },
{ path: 'a/c', component: CComponent },
{ path: 'a/:id', component: AComponent },

【讨论】:

  • 不敢相信我错过了这个简单的事情。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 2018-10-15
  • 1970-01-01
  • 2018-12-28
  • 1970-01-01
相关资源
最近更新 更多