2

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,
    },
  ]
}
jwa
  • 3,239
  • 2
  • 23
  • 54
Свободен Роб
  • 2,579
  • 2
  • 20
  • 26
  • 1
    You can do this with a path matcher function instead of specifying a static path: See https://angular.io/guide/routing-with-urlmatcher and https://stackoverflow.com/a/56391974/1751497 – rooby Oct 12 '21 at 01:29

3 Answers3

7

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,579
  • 2
  • 20
  • 26
3

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

Madhu Ranjan
  • 17,334
  • 7
  • 60
  • 69
1

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

AngularChef
  • 13,797
  • 8
  • 53
  • 69