以角度路由 - 路径的顺序是否重要?

时间:2018-04-20 19:10:46

标签: angular angular-routing

app.module.ts文件中列出的路径顺序是否重要? 例如......

   RouterModule.forRoot([
      {path:'',component:HomeComponent},
      {path:'followers',component:GithubFollowersComponent},
      {path:'followers/:username/:userid',component:GithubProfileComponent},
      {path:'posts',component:PostsComponent},
      {path:'**',component:NotFoundComponent}
    ])

VS ..

  RouterModule.forRoot([
      {path:'',component:HomeComponent},
      {path:'followers/:username/:userid',component:GithubProfileComponent},
      {path:'followers',component:GithubFollowersComponent},
      {path:'posts',component:PostsComponent},
      {path:'**',component:NotFoundComponent}
    ])

我正在看一个教程,它说订单确实很重要..但我尝试了两种方式,它们似乎都按预期工作......

如果我将外卡路径(**)移到顶部,那么我确实注意到了差异。 但对于其他人来说,订单根本不重要吗? 或者我在这里遗漏了什么?....

1 个答案:

答案 0 :(得分:3)

其他路径完全不同,所以不,顺序对于这些并不重要。路由引擎不会混淆followersfollowers/:username/:userid - 因为the Angular guide指出,:username:userid是必需参数,因此需要存在,如在followers/testuser/10

当两条路线发生冲突时很重要,如posts**。路径/posts将由两条路线匹配,第一条路线将获胜。

这就是通配符最后的原因。作为一项基本规则,请始终尝试按最具体到最不具体的顺序排序。

相关问题