angular 中的路由 - 路径的顺序重要吗?
Routing in angular - Does the order of paths matter?
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}
])
对比..
RouterModule.forRoot([
{path:'',component:HomeComponent},
{path:'followers/:username/:userid',component:GithubProfileComponent},
{path:'followers',component:GithubFollowersComponent},
{path:'posts',component:PostsComponent},
{path:'**',component:NotFoundComponent}
])
我在看一个教程,它说顺序很重要..但我尝试了两种方法,它们似乎都按预期工作...
如果我将通配符路径 ( ** ) 移动到顶部,那么是的,我确实注意到了差异。
但是对于其他人来说,顺序根本不重要吗?
还是我在这里遗漏了什么?....
其他路径完全不同,所以不,顺序无关紧要。路由引擎不会混淆 followers
和 followers/:username/:userid
- 正如 the Angular guide 指出的那样,:username
和 :userid
是必需的参数,因此需要存在,因为在 followers/testuser/10
.
当两条路线发生冲突时,确实很重要,例如posts
和**
。路径 /posts
将被两条路线匹配,第一个获胜。
这就是通配符在末尾的原因。作为一项基本规则,始终尝试按最具体到最不具体的顺序排序。
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}
])
对比..
RouterModule.forRoot([
{path:'',component:HomeComponent},
{path:'followers/:username/:userid',component:GithubProfileComponent},
{path:'followers',component:GithubFollowersComponent},
{path:'posts',component:PostsComponent},
{path:'**',component:NotFoundComponent}
])
我在看一个教程,它说顺序很重要..但我尝试了两种方法,它们似乎都按预期工作...
如果我将通配符路径 ( ** ) 移动到顶部,那么是的,我确实注意到了差异。 但是对于其他人来说,顺序根本不重要吗? 还是我在这里遗漏了什么?....
其他路径完全不同,所以不,顺序无关紧要。路由引擎不会混淆 followers
和 followers/:username/:userid
- 正如 the Angular guide 指出的那样,:username
和 :userid
是必需的参数,因此需要存在,因为在 followers/testuser/10
.
当两条路线发生冲突时,确实很重要,例如posts
和**
。路径 /posts
将被两条路线匹配,第一个获胜。
这就是通配符在末尾的原因。作为一项基本规则,始终尝试按最具体到最不具体的顺序排序。