【问题标题】:Angular invalid configuration of main route (empty path)主路由的角度无效配置(空路径)
【发布时间】:2018-01-25 21:39:41
【问题描述】:

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

这是我的代码:

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

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

【问题讨论】:

    标签: angular redirect routing angular-routing angular-router


    【解决方案1】:

    问题是空路由缺少pathMatch 属性,默认为prefix

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

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

    原因如下:

    从技术上讲,pathMatch = 'full' 在 剩余的,不匹配的 URL 段匹配 ''。在这个例子中, 重定向位于顶级路由中,因此剩余的 URL 和 整个网址都是一样的。

    另一个可能的 pathMatch 值是'prefix',它告诉路由器 当剩余的 URL 以 重定向路由的前缀路径。

    不要在这里这样做。如果 pathMatch 值为“前缀”,则每个 URL 将匹配 ''。

    更多详情请参考官方文档:https://angular.io/guide/router#redirecting-routes

    【讨论】:

    • @AndreiMatracaru Stackoverflow 允许我仅在 2 天后接受自己的答案
    猜你喜欢
    • 2018-08-04
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 2021-08-26
    • 1970-01-01
    相关资源
    最近更新 更多