Route without parameter value; Angular route redirection - Can I use redirectTo for parameter path?

I wander if you can redirect the path: '' to path: ': id'? Is there any way I can achieve this?

Thanks.

{ path: 'folder', children: [ { path: '', redirectTo: ':id', pathMatch: 'full', }, { path: ':id', canActivate: [AuthGuard], component: DocumentsComponent, }, ] } 
+1
source share
3 answers

I found a way to redirect the "folder" route to "folder /: id" with an empty id parameter:

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

* Please note that redirection must be first.

+2
source

if you specify the default id, say 1 and redirectTo '1' as shown below, it should work,

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

Hope this helps!

+1
source

I do not think you can.

The redirectTo property must be a string, and you will need to pass an array of link parameters with a value for the parameter :id .

This is a bit hacky, but you can redirect the empty path to the hard-coded path associated with the component that redirects to ':id' programmatically (using router.navigate() ) ...

0
source

Source: https://habr.com/ru/post/1257409/


All Articles