2
votes

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.

1

1 Answers

14
votes

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'));
}