没有参数值的路由; Angular 路由重定向 - 我可以将 redirectTo 用于参数路径吗?

Route without parameter value; Angular route redirect - Can I use redirectTo to parameter path?

我在想是否可以将路径:“ ”重定向到路径:“:id”?有什么办法可以实现吗?

谢谢。

{
  path: 'folder',
  children: [
    {
      path: '',
      redirectTo: ':id',
      pathMatch: 'full',
    },
    {
      path: ':id',
      canActivate: [AuthGuard],
      component: DocumentsComponent,
    },
  ]
}

我认为你做不到。

redirectTo 属性 必须是字符串,您需要传递一个 link 参数数组 :id参数

这有点老套,但您可以将空路径重定向到与组件关联的 hard-coded 路径,该组件以编程方式重定向到 ':id'(使用 router.navigate())...

如果你给一个默认的 id 让我们说 1 和 redirectTo '1' 就像下面它应该工作,

{
  path: 'folder',
  children: [
    {
      path: '',
      redirectTo: '1',
      pathMatch: 'full',
    },
    {
      path: ':id',
      canActivate: [AuthGuard],
      component: DocumentsComponent,
    },
  ]
}

希望对您有所帮助!!

我找到了一种使用空 'id' 参数将 'folder' 路由重定向到 'folder/:id' 的方法:

{
    path: 'folder',
    redirectTo: 'folder/',
    pathMatch: 'full',
},
{
    path: 'folder',
    children: [
        {
            path: ':id',
            canActivate: [AuthGuard],
            component: DocumentsComponent,
        },
    ]
}

*注意必须先重定向。