嗨 Angular 开发人员,
请我需要您的帮助,我需要阻止具有特定角色的路由,这是我的带有配置文件的文档:
我的角度守卫返回并反对配置文件:{admin:true,current:false}
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from '../services/auth.service';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class RoleGuard implements CanActivate {
constructor(
public authService: AuthService,
public router: Router
) { }
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<any> | boolean {
return this.authService.getAuthWithProfile().pipe(
map((data) => {
const roles = data.roles;
return roles; // This returns {admin: true, current: false}
})
);
}
}
问题是如何在 Angular 路由中实现角色保护,例如:
{ path: 'tasks', component: TasksComponent, canActivate: [RoleGuard] },