18

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?

danday74
  • 52,471
  • 49
  • 232
  • 283
  • Not sure there's a built-in way of doing that, but you can always refer to the `Promise` returned from the `.navigate` function. – Amit Mar 13 '17 at 12:21
  • thanks - not the answer i am looking for but deffo the best approach suggested so far :D – danday74 Mar 13 '17 at 13:20
  • https://stackoverflow.com/questions/50850125/check-if-a-route-exist-in-angular-2 – danday74 Apr 01 '19 at 12:55

3 Answers3

17

You can check if the route exists like that using the promise way :

this.router.navigate(['redirect'])
  .then(data => {
    console.log('Route exists, redirection is done');
  })
  .catch(e => {
    console.log('Route not found, redirection stopped with no error raised');
  });

If the route is defined, redirection will happen. If not, you can execute some code in the catch block.

Theo Godard
  • 348
  • 1
  • 7
2

I have the handling for preventing for the urls that doesn't exist

{path: 'dashboard', component: DashboardComponent}
{path: 'demo/:demo', component: DemoComponent},
{path: '**', redirectTo: '/dashboard'}

** means all the paths that are not able to redirect

mayur
  • 3,558
  • 23
  • 37
  • i want to know if the route stored by this.authService.redirectUrl exists - thanks anyway for trying – danday74 Mar 13 '17 at 13:19
  • you mean recognize path check the full url before navigate if correct.. can check here https://angular.io/docs/ts/latest/guide/router.html – mayur Mar 13 '17 at 13:33
-1

Maybe with something like this :

redirectUrl: string;

this.redirectUrl = this.authService.redirectUrl ? this.authService.redirectUrl : '/home';
this.router.navigate([this.redirectUrl]);

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 :

if(this.authService.redirectUrl){
   //do what you want
}
mickdev
  • 2,765
  • 11
  • 15
  • sorry i want to check if the route stored by this.authService.redirectUrl exists - i know that this.authService.redirectUrl is set - thanks anyway for trying – danday74 Mar 13 '17 at 13:19
  • my first answer check if the route exist and set a default value if not. If you just want to check if it exist, take a look to my updated answer. – mickdev Mar 13 '17 at 13:24
  • if value of this.authService.redirectUrl === "/fred" then it will try to navigate to "/fred" but "/fred" does not exist. how do i check if "/fred" exists? – danday74 Mar 13 '17 at 13:26
  • Ah ok ! I think you can't do that natively. If you have few routes, you can create an array and check if redirectUrl is inside then redirect if everything is ok. But you are the one who are setting redirectUrl in the first place, so why checking again ? – mickdev Mar 13 '17 at 13:31
  • the user could manually type in the URL /fred - then if the code got fired at that point it would set /fred as the redirectUrl - anyway not to worry - i have accepted your original approach - thanks :) – danday74 Mar 13 '17 at 14:35