I have a navigation with three submenus and their according children routes. Now some of the submenus are not visible (ngIf), depending on the claims a user got from the server.
Wenn the main menu is clicked, I redirect to one of the submenus but sometimes this submenu is not accessible - then I would like to redirect to the next sibling:
{
path: 'mymainmenue',
children: [
{ path: 'submenu1', component: SubMenu1Component },
{ path: 'submenu2', component: SubMenu2Component },
{ path: 'submenu3', component: SubMenu3Component },
{ path: '', redirectTo: 'submenu1' },
]
}
Now the "sometimes" I'm able to calculate. I've tried to create three routes with an empty path ('') and a CanActivate
-Guard. Similar to this:
{
path: 'mymainmenue',
children: [
{ path: 'submenu1', component: SubMenu1Component },
{ path: 'submenu2', component: SubMenu2Component },
{ path: 'submenu3', component: SubMenu3Component },
{ path: '', redirectTo: 'submenu1', canActivate: [ClaimsGuard] },
{ path: '', redirectTo: 'submenu2', canActivate: [ClaimsGuard] },
{ path: '', redirectTo: 'submenu3', canActivate: [ClaimsGuard] },
]
}
hoping I could use the route and my rules to return false
in case for example the submenu1 is invisible and the router would chose the next possible route (redirect to submenu2).
The problem with my approach is that redirectTo
gets executed even before my canActivate
-method is called.
What would be the best way to do this?