gpt4 book ai didi

angular - 如何将参数传递给canActivate的构造函数?

转载 作者:行者123 更新时间:2023-12-01 10:21:50 24 4
gpt4 key购买 nike

我有以下路线路径:

{
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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com