canLoad
and canActivate
@select
Question: Why does canActivate
work with the observable that @select
returns while canLoad
breaks all routing from then on? What is the difference between the two guards?
Related angular issue: https://github.com/angular/angular/issues/18991
auth.guard.ts
@Injectable()
export class AuthGuard implements CanLoad, CanActivate {
@select() readonly authenticated$: Observable<boolean>; // @angular-redux/store
canLoad(): Observable<boolean> | boolean {
// return true; // works
return this.authenticated$; // ERROR: all routing stops from and to the current page
}
canActivate(): Observable<boolean> | boolean {
// return true; // works
return this.authenticated$; // works
}
}
app-routing.module
const routes: Routes = [
{
path: '',
component: SomeAComponent,
pathMatch: 'full'
},
{
path: 'someb',
component: SomeBComponent,
canActivate: [
AuthGuard
],
},
{
path: 'lazy',
loadChildren: './lazy/lazy.module#LazyModule',
canLoad: [
AuthGuard
]
},
{
path: '**',
redirectTo: '/'
}
];
The same issue I had, so to resolve it and let working in both CanLoad and CanActivate, you should add operator take(1)
@Injectable()
export class AuthGuard implements CanLoad, CanActivate {
@select() readonly authenticated$: Observable<boolean>; // @angular-redux/store
canLoad(): Observable<boolean> | boolean {
// return true; // works
return this.authenticated$.pipe(take(1));
}
canActivate(): Observable<boolean> | boolean {
// return true; // works
return this.authenticated$;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With