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

276
Vistas
Angular - Edit route variables by name

in my Angular 12 project I have routes like these:

 /:par1/:par2/something/page
 /:par1/:par2/something2/page2

I need to set par1 and par2 programmatically.

Right now I use this code to achieve that:

const url = this.router.routerState.snapshot.url;
const splittedUrl = url.split('/');
splittedUrl[1] = getPar1();
splittedUrl[2] = getPar2();
this.router.navigate(splittedUrl);

but I find it a really ugly solution.

Is there a way to set par1 and par2 by their name like we do when we get them from the activated route?

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

0

I solved today the same problem.

This is my approach:

import ActivatedRoute into the constructor

constructor(private activatedRoute: ActivatedRoute)

use activatedRoute for getting the urlSegment and map an array with all segments (apply your change where needed)

This is an example applied at your problem

const url: string[] = this.activatedRoute.snapshot['_urlSegment'].segments.map((urlSegment: UrlSegment, index: number) => {
      if (index === 0) return getPar1();
      else if (index === 1) return getPar2();
      else return urlSegment.path;
});
this.router.navigate(url);
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