本文介绍了如何将参数传递给canActivate的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下路线路径:
{
path: 'teacher',
component: DashboardTeacher, canActivate: [AccessGuardTeacher('Teacher')]
}
如您所见,我试图在类 AccessGuardTeacher
中传递参数'Teacher':
As you can see I tried to pass parameter 'Teacher' in class AccessGuardTeacher
:
export class AccessGuardTeacher implements CanActivate {
constructor(private role: string) {
}
}
该怎么做对呢?
推荐答案
您的路线应配置为:
{
path: 'teacher',
component: DashboardTeacherComponent,
canActivate: [AccessGuardTeacher],
data: {
role: 'teacher'
}
}
在CanActivate Guard中获取路线数据
Get your route data in your CanActivate Guard
export class AccessGuardTeacher implements CanActivate {
constructor() {
}
canActivate(route: ActivatedRouteSnapshot): boolean {
const role = route.data.role;
return true; //based on your condition
}
}
这篇关于如何将参数传递给canActivate的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!