My understanding is that the call signature of canActivate
is as follows:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
}
I have a service that requires the name of a component and returns the necessary user role to access it. This allows me to check in the canActivate
function of my guard whether the active user has the required role.
The issue I am facing is that I am unsure how to access the component. My research led me to solutions like this:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const nameOfComponent = route.routeConfig.component.name;
return doSomeRoleCheck(nameOfComponent);
}
However, when implementing this code, I encounter an error: "Cannot read proprty 'name' of undefined". How can I retrieve the component of the current Route and specifically its name as a string?
Edit
I have discovered that this guard is being checked on a parent route. It works when checking on a child route. How can I access the child component from the parent's ActivatedRouteSnapshot
?