问题可能在于
CanLoad
。
CanLoad
盖尔德保护了一个
module
加载但一次
module
然后加载
CanLoad
后卫什么也没做。
例如,假设用户登录了应用程序并导航到某个模块。之后他点击退出。现在,如果用户想要,他将能够导航到相同的模块,因为它已经加载。
因此,如果您想保护您的应用程序,最好使用
CanActivate
。
加
CanActivate
进入你的RoleGaurd
import { CanLoad, CanActivate, Route,Router,
ActivatedRouteSnapshot, RouterStateSnapshot } from ‘@angular/router’;
import { AuthenticationService } from “../_services”;
import { Injectable } from “@angular/core”;
@Injectable({ providedIn: ‘root’ })
export class RoleGuard implements CanLoad, CanActivate {
constructor(private authService: AuthenticationService,private router: Router) { }
canLoad(route: Route) {
let authorities = route.data.roles;
if (this.authService.hasAnyRole(authorities)) {
return true;
}
return false;
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
let authorities = route.data.roles;
if (this.authService.hasAnyRole(authorities)) {
return true;
}
return false;
}
}
</code>