Fra*_*rzi 3 redirect routing angular-routing angular angular-router
在我的路由器配置中,我DashboardComponent在路径下配置了一个'dashboard',我想在没有指定路径时自动将用户重定向到此路由(空路径,所以只是/)。
这是我的代码:
const appRoutes: Routes = [
{ path: '', redirectTo: 'dashboard'},
{ path: 'dashboard', component: DashboardComponent },
/* other routes... */
];
Run Code Online (Sandbox Code Playgroud)
未处理的承诺拒绝:路由 '{path:"", redirectTo:"dashboard"}' 的配置无效:请提供 'pathMatch'。'pathMatch' 的默认值是 'prefix',但通常意图是使用 'full'
问题是缺少pathMatch空路由的属性,默认为prefix.
但是,在这种情况下,该pathMatch值应设置为full:
const appRoutes: Routes = [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full'},
{ path: 'dashboard', component: DashboardComponent },
/* other routes... */
];
Run Code Online (Sandbox Code Playgroud)
原因如下:
从技术上讲, pathMatch = 'full' 会在 URL 的其余未匹配段匹配 '' 时导致路由命中。在此示例中,重定向位于顶级路由中,因此其余 URL 和整个 URL 是相同的。
另一个可能的 pathMatch 值是 'prefix',它告诉路由器在剩余的 URL 以重定向路由的前缀路径开头时匹配重定向路由。
不要在这里这样做。如果 pathMatch 值为 'prefix',则每个 URL 都将匹配 ''。
更多细节参考官方文档:https : //angular.io/guide/router#redirecting-routes
| 归档时间: |
|
| 查看次数: |
4251 次 |
| 最近记录: |