I want to know how to find out all the routes which are not protected with a guard in angular 2? I know that adding this property "canActivate : [myGuard]" in routes will call the canActivate method of my guard. But how to make a list of all the routes which don't have this property?
- I don't get it, if you can access your appRoutes array what's the impediment to get the routers you need ?Hosar– Hosar2018-04-22 23:05:20 +00:00Commented Apr 22, 2018 at 23:05
- @Hosar - I need to access this information in a different module(core module). This core module has a guard and every time a protected route is hit, I get a callback in the canActivate but I don't know how many routes are unguarded/unprotected.user911– user9112018-04-23 01:10:01 +00:00Commented Apr 23, 2018 at 1:10
Add a comment |
1 Answer
I would have pass a key in the path data telling me whether it is guarded or not if i were you. Like below.
const appRoutes: Routes = [ { path: 'crisis-center', component: CrisisListComponent , canActivate: [YourCanActivateClass] }, { path: 'hero/:id', component: HeroDetailComponent, canActivate: [YourCanActivateClass] }, { path: 'heroes', component: HeroListComponent, data: { isGuaded: false } }, ]; And in your individual component you can
constructor(route: ActivatedRoute) { isGuaded= this.route.snapshot.data['isGuaded']; if(isGuaded === false) { //Your login here } } This is another way.
1 Comment
user911
-Thanks for your answer. I also thought about this approach, the issue is that if I have 15 routes and only 2 are guarded and rest are not, I will end up adding isGuard=false to remaining 13. I was looking for a better approach.