2

I made a guard to conditionally redirect from the homepage, in the app.routing.ts:

{ path: '', redirectTo: '/dashboard', pathMatch: 'full', canActivate: [HomePageGuard] }

My guard looks something like this:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if(condition){
  return true;
} else {
  this.router.navigate(['/info']);
  return false;
}
}

Now for every other path, this works great. But for

{ path: '', redirectTo: '/dashboard', pathMatch: 'full'}

The guard doesn't seem to do anything, it always redirects to /dashboard, no matter what the guard returns. I don't want to put the guard into the /dashboard path, because this path should still be available for everyone, just not as the start page...

Any help would be greatly appreciated

edit: How i solved it for now:

{ path: '', component: HomePageComponent, canActivate: [HomePageGuard] }

(note that the HomePageComponent is empty, since it gets redirected in the [HomePageGuard] either way)

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if(condition){
  this.router.navigate(['/dashboard']);
  return false;
} else {
  this.router.navigate(['/start']);
  return false;
}
}

1 Answers1

0

As mentioned here, the guard isn't invoked at all when a redirect is specified.

Harish KM
  • 1,303
  • 7
  • 17