您好我遇到了问题.当我尝试导航到不被允许的页面时,我的CanActivate警卫会被调用两次,因为我没有登录.
我有1个根模块,并提供了我的CanActivate后卫和其他服务.
先感谢您!
这是我的路由器:
const appRoutes: Routes = [
{
path: "",
pathMatch: "full",
redirectTo: "/meal-list",
},
{
path: "login",
component: LoginComponent,
},
{
path: "meal-list",
component: MealListComponent,
canActivate: [AuthActivateGuard],
}
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes, {useHash: true});
Run Code Online (Sandbox Code Playgroud)
守卫:
@Injectable()
export class AuthActivateGuard implements CanActivate {
constructor(private authService: AuthService,
private router: Router) {
console.log("guard created");
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>|boolean {
if (!this.authService.authenticated) {
return this.authService.checkLogged().map(res => {
this.authService.authenticated = true;
return true;
}).catch(()=> {
this.authService.authenticated = …Run Code Online (Sandbox Code Playgroud)