Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 get current route in guard

I have an AccessGuard class in my project which its work is to determine if user can access to the route or not. I used the router.url to get the current route but the url returns the route before navigation to the new route like I'm in the users route and I click on the candidates route so the url returns users instead of candidates which I want that to validate access to the route this is my route file:

const routes:Routes = [ {     path:'',     component:PanelComponent,     canActivate:[AuthGuard,AccessGuard],     canActivateChild:[AuthGuard,AccessGuard],     children:[         {             path:'dashboard',             component:DashboardComponent         },         {             path:'users',             component:UsersComponent         },         {             path:'users/:id',             component:ShowUserComponent         },         {             path:'candidates',             component:CandidatesComponent         },         {             path:'permissions',             component:PermissionsComponent         },         {             path:'holidays',             component:HolidaysComponent         },         {             path:'candidate/:id',             component:CandidateComponent         },         {             path:'salary/create',             component:InsertSalaryComponent         },         {             path:'document/create',             component:InsertDocumentComponent         },         {             path:'leave/create',             component:InsertLeaveComponent         }     ] }  ]; 

and this is my access guard:

permissions; currentRoute; constructor(private authService:AuthService,private router:Router){     this.permissions = this.authService.getPermissions(); }  canActivate(){     return this.checkHavePermission(); }  canActivateChild(){     console.log(this.router.url);     return this.checkHavePermission(); }  private checkHavePermission(){     switch (this.router.url) {         case "/panel/users":             return this.getPermission('user.view');         case '/panel/dashboard':             return true;         case '/panel/candidates':             return this.getPermission('candidate.view');         default:             return true;     } }   getPermission(perm){     for(var i=0;i<this.permissions.length;i++){         if(this.permissions[i].name == perm ){             return true;         }     }     return false; } 
like image 758
Hossein Ahmadi Avatar asked Sep 28 '16 12:09

Hossein Ahmadi


People also ask

How do I get current URL in app component?

you can subscribe to router. events and filter for NavigationEnd event to get the current active route url.

What is Angular route guard?

Angular route guards are interfaces provided by Angular which, when implemented, allow us to control the accessibility of a route based on conditions provided in class implementation of that interface. Here are some types of Angular guards: CanActivate, CanActivateChild, CanLoad, CanDeactivate and Resolve.

What are route Guards in Angular 2?

The Angular router's navigation guards allow to grant or remove access to certain parts of the navigation. Another route guard, the CanDeactivate guard, even allows you to prevent a user from accidentally leaving a component with unsaved changes.

How canActivate works in Angular?

The canActivate is like a constructor. It will be called before accessing the routes. The canActivate has to return true to access the page. If it returns false, we can not access the page.


1 Answers

You need to use the method parameters to see the target route:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {   console.log(state.url);//'candidates' }  canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot) 
like image 152
Guilherme Meireles Avatar answered Sep 19 '22 19:09

Guilherme Meireles