我有以下路线:
{
path: 'teacher',
component: DashboardTeacher, canActivate: [AccessGuardTeacher('Teacher')]
}
如您所见,我尝试在课程AccessGuardTeacher
中传递参数'Teacher':
export class AccessGuardTeacher implements CanActivate {
constructor(private role: string) {
}
}
如何做到这一点?
答案 0 :(得分:2)
您的路线应配置为:
{
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
}
}
答案 1 :(得分:1)
try this way:
{
path: 'teacher',
component: DashboardTeacher, canActivate: [AccessGuardTeacher],
data: {type: ['Teacher']}
}
export class AccessGuardTeacher implements CanActivate {
constructor(private role: string) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
let type = route.data["type"] as Array<string>;
}
}