我刚开始使用angular6,目前在使用angular提供的基于用户角色的不同页面时遇到了问题。我在应用程序中有3个主要角色
这是我的AuthGard auth.guard.ts
export class AuthGuard implements CanActivate {
constructor(private auth: AuthService,
private router: Router) { }
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> |
boolean {
console.log(this.auth.isLoggedIn())
if (this.auth.isLoggedIn()) {
return true;
}
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } })
return false;
}
}
这是我的角色守卫role.guard.ts
export class RoleGuard implements CanActivate {
constructor(private _auth: AuthService, private _router: Router) { }
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
switch (this._auth.getRole()) {
case "STUDENT":
this._router.navigate(['/']);
return true;
case "DEPARTMENT":
this._router.navigate(['/department']);
return true;
case "FACULTY":
this._router.navigate(['/faculty']);
return true;
default:
return false;
}
}
}
和我的模块student.module.ts
{ path: '', component: DashboardComponent, canActivate: [AuthGuard, RoleGuard] },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard, RoleGuard] }
我遗漏了什么?
发布于 2019-07-19 18:01:01
在使用angular 6和更高版本时,请确保不要将它们包含在提供程序根中,因为服务已经是根:‘providedIn’。例如:
@Injectable({
providedIn: 'root'
})
https://stackoverflow.com/questions/52166256
复制