Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

376
Vistas
How to test if a route exists in Angular2?

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?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

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

over 4 years ago · Santiago Trujillo Denunciar

0

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
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda