cagliostro
cagliostro

Reputation: 192

Custom Guard, get ID Route Params

Wondering if we can get the :id of a route in the guard.

Example :

{path: ':id/admin', component: AdminComponent, canActivate: [AdminGuard], data:{restricted: x}}

if User Reload I need to instantiate before some params to know if he is admin or not and de facto need the :id of this route URL I would like also to come back to url/:id if he is not allowed.

Upvotes: 4

Views: 10647

Answers (1)

funkizer
funkizer

Reputation: 4897

Yes, first argument of canActivate is a snapshot of the route you're currently checking.

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
   console.log(route.paramMap.get('id'));
}

Upvotes: 19

Related Questions