By-passing Route Guard: Angular 5

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



By-passing Route Guard: Angular 5



I'm using a route guard which is taking care of all the routes of one of my module. But, in a particular scenario where I know the route I'm accessing is a valid route, I would like to disable the route guard in that case. I'm using:


router.navigate(url)



for navigating to that url but would like to by pass the route guard in this case. Is there a standard or 'best-practice' way to do this?





You can add the check for exceptional valid route in your guard itself and simply return if that route is found.
– Amit Chigadani
Aug 8 at 18:22


exceptional valid route





Thanks for the help @AmitChigadani. But cannot do that because the 'id' in the route will be dynamic, and I don't want to remove route guard check from that route completely as it may be invalid when in the future. But the use case I described for sure knows that the route will be valid. So I would like to by pass only in that case.
– Aiguo
Aug 8 at 18:24






You cannot disable route for a scenario. You can completely remove it from the route which you don't want. Suggestion is allow to execute route guard and return true in canActivate method
– Suresh Kumar Ariya
Aug 8 at 18:35




1 Answer
1



You can use custom conditions in your guard like so :


canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot)
: boolean | Observable<boolean> | Promise<boolean>
if (route.url === 'unique/route/not/to/guard')
return true;
else
// ... Your logic




If your route is complicated (with ids and whatnot), consider using regexp.exec to check the validity.


regexp.exec






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard