In Angular2 how do I check if a route exists?
I have a method that remembers which route to navigate to if the user is not authorised.
On login I have:
this.router.navigate([this.authService.redirectUrl]);
But I only want to navigate IF the redirectUrl references a valid route, something like ..
if (this.authService.redirectUrl is a valid route) {
this.router.navigate([this.authService.redirectUrl]);
}
Is there any way to check this?
I have the handling for preventing for the urls that doesn't exist
**
means all the paths that are not able to redirectMaybe with something like this :
This will check if the redirect url exist, if not it will redirect to the alternative route
/home
(you can change home to whatever you like)Edit: If you just want to check if
this.authService.redirectUrl
, you can try something like this :You can check if the route exists like that using the promise way :
If the route is defined, redirection will happen. If not, you can execute some code in the catch block.