Search code examples
angularangular5

How to pass parameters to constructor of canActivate?


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?


Solution

  • Angular 14+ Solution

    {
        path: 'edit',
        component: Cmp,
        canActivate: [
          () =>
            inject(Service).hasUserPermissions(['WRITE_SOMETHING'])
              ? true
              : inject(Router).parseUrl('/goSomewhere'),
        ],
      },