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

假装没事ソ 提交于 2020-01-03 02:47:12

问题


I'm wandering if it is possible to redirect path: ' ' to path: ':id' ? Is there a way I can achieve this?

Thank you.

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

回答1:


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.




回答2:


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!!




回答3:


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())...



来源:https://stackoverflow.com/questions/41899090/route-without-parameter-value-angular-route-redirect-can-i-use-redirectto-to

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