首页 文章

主路径的角度无效配置(空路径)

提问于
浏览
0

在我的路由器配置中,我在路径 'dashboard' 下配置了 DashboardComponent ,并且我想在没有指定路径时自动将用户重定向到此路由(空路径,所以只需 / ) .

这是我的代码:

const appRoutes: Routes = [
  { path: '', redirectTo: 'dashboard'},
  { path: 'dashboard', component: DashboardComponent },
  /* other routes... */
];

未处理的承诺拒绝:路由'{path:“”,redirectTo:“dashboard”}'的配置无效:请提供'pathMatch' . 'pathMatch'的默认值是'prefix',但通常意图是使用'full'

1 回答

  • 2

    问题是缺少空路线的 pathMatch 属性,默认为 prefix .

    但是在这种情况下, pathMatch 值应设置为 full

    const appRoutes: Routes = [
      { path: '', redirectTo: 'dashboard', pathMatch: 'full'},
      { path: 'dashboard', component: DashboardComponent },
      /* other routes... */
    ];
    

    原因如下:

    从技术上讲,当URL的剩余不匹配段与''匹配时,pathMatch ='full'会导致路由命中 . 在此示例中,重定向位于顶级路由中,因此剩余的URL和整个URL是相同的 . 另一个可能的pathMatch值是'prefix',它告诉路由器在剩余URL以重定向路由的前缀路径开始时匹配重定向路由 . 不要在这里这样做 . 如果pathMatch值为'prefix',则每个URL都匹配'' .

    有关更多详细信息,请参阅官方文档:https://angular.io/guide/router#redirecting-routes

相关问题