0

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?

2
  • I don't get it, if you can access your appRoutes array what's the impediment to get the routers you need ? Commented 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. Commented Apr 23, 2018 at 1:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

-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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.