Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

364
Views
¿Cómo probar si existe una ruta en Angular2?

En Angular2, ¿cómo verifico si existe una ruta?

Tengo un método que recuerda a qué ruta navegar si el usuario no está autorizado.

Al iniciar sesión tengo:

 this.router.navigate([this.authService.redirectUrl]);

Pero solo quiero navegar SI el redirectUrl hace referencia a una ruta válida, algo así como...

 if (this.authService.redirectUrl is a valid route) { this.router.navigate([this.authService.redirectUrl]); }

¿Hay alguna manera de verificar esto?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede verificar si la ruta existe así usando la forma de promesa:

 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'); });

Si la ruta está definida, se producirá la redirección. Si no, puede ejecutar algún código en el bloque catch.

about 4 years ago · Santiago Trujillo Report

0

tengo el manejo para prevenir por las urls que no existe

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

** significa todos los caminos que no pueden redirigir

about 4 years ago · Santiago Trujillo Report

0

Tal vez con algo como esto:

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

Esto verificará si existe la URL de redirección, si no, se redirigirá a la ruta alternativa /home (puede cambiar a la dirección que desee)

Editar: si solo desea verificar si this.authService.redirectUrl , puede intentar algo como esto:

 if(this.authService.redirectUrl){ //do what you want }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!