Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can "canActivate" received data?

I have these lines in my router:

..
    canActivate: [MyGuard], 
    path: "search",
    component: SearchComponent,
    data: {
        accessRoles: [roleAdmin, roleUser]
    }
..

I want to limit access to SearchComponent by canActivate of MyGuard.

Is it possible to get this data array from canActivate?

Thank you in advance!

like image 662
Egor Avatar asked Sep 09 '16 10:09

Egor


People also ask

What happens when canActivate returns false?

CanActivatelink If any guard returns false , navigation is cancelled. If any guard returns a UrlTree , the current navigation is cancelled and a new navigation begins to the UrlTree returned from the guard.

What is the difference between canActivate and CanDeactivate?

CanActivateChild - Decides if children routes of a route can be activated. CanDeactivate - Decides if a route can be deactivated.

What is the use of canActivate in Angular?

The canActivate guard checks if the user can visit the specific route or we have to prevent access to that specific route. We use the this guard when we have to check some condition and based on that users have the access to reach that specific route or not, before activating the component or showing it to the user.

What does the canActivate () method return when configuring guards in Angular?

CanActivate. Guards are implemented as services that need to be provided so we typically create them as @Injectable classes. Guards return either true if the user can access a route or false if they can't.


1 Answers

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
  console.log(route.data)
}
like image 178
Günter Zöchbauer Avatar answered Nov 15 '22 05:11

Günter Zöchbauer