I have the following route path:
{
path: 'teacher',
component: DashboardTeacher, canActivate: [AccessGuardTeacher('Teacher')]
}
As you can see I tried to pass parameter 'Teacher' in class AccessGuardTeacher
:
export class AccessGuardTeacher implements CanActivate {
constructor(private role: string) {
}
}
How to do that right?
Your route should be configured like:
{
path: 'teacher',
component: DashboardTeacherComponent,
canActivate: [AccessGuardTeacher],
data: {
role: 'teacher'
}
}
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
}
}
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>;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With