我有以下路线路径:
{
path: 'teacher',
component: DashboardTeacher, canActivate: [AccessGuardTeacher('Teacher')]
}
如您所见,我尝试在类
AccessGuardTeacher
中传递参数'Teacher':export class AccessGuardTeacher implements CanActivate {
constructor(private role: string) {
}
}
怎么做对吗?
最佳答案
您的路线应配置为:
{
path: 'teacher',
component: DashboardTeacherComponent,
canActivate: [AccessGuardTeacher],
data: {
role: 'teacher'
}
}
在您的CanActivate Guard中获取路线数据
export class AccessGuardTeacher implements CanActivate {
constructor() {
}
canActivate(route: ActivatedRouteSnapshot): boolean {
const role = route.data.role;
return true; //based on your condition
}
}
关于angular - 如何将参数传递给canActivate的构造函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50624086/