phacic
phacic

Reputation: 1542

Router canActivate with more than 1 guard

Does angular(v 4.1.1) router canActivate take more than one function

{
   path: '',
   component: SomeComponent,
   canActivate: [guard1, guard2, ...]
}

should something like that work? If not they why would it be in a list if its suppose to take just one guard

Because I have something similar and even though guard1 returns false, guard2 will still be executed.

Thanks in advance

Angular 4.1.1

Upvotes: 6

Views: 4224

Answers (1)

Vladimir Zdenek
Vladimir Zdenek

Reputation: 2290

This should work but I believe the guards are executed in parallel not in a sequence. So the second one does not wait until the first one return a value. This should not really affect you if your guards are synchronous, but if they are asynchronous, you will run into this "issue".

If you need your guards to depend on each other, you could separate the common part of the check and all your guards could call that logic. But I think in most cases this should not even be necessary, because if only one of them fails, the route is not activated.

Upvotes: 4

Related Questions