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

邮差的信 提交于 2019-12-07 22:46:32

I found a way to redirect 'folder' route to 'folder/:id' with empty 'id' parameter:

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

*Note that the redirect must be first.

if you give a default id lets say 1 and redirectTo '1' like below it should work,

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

Hope this helps!!

I don't think you can.

The redirectTo property must be a string, and you would need to pass a link parameters array with a value for the :id param.

It's a bit hackish, but you could redirect the empty path to a hard-coded path associated to a component that redirects to ':id' programmatically (using router.navigate())...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!