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

234
Vistas
Why does angular router url doesn't have the same value as window.location.href

Was working on my website and had to do some request to the api while instantiating a root services.

I wanted to get some data based on the url, so I did the following

@Injectable({
  providedIn: 'root',
})
export class MyService {

  constructor(private _router: Router) {
     this.init() 
  }

  init() {
    console.log('url', this._router.url, window.location.href)
    // this._router.url : `/`
    // window.location.href : `http://localhost:4201/my/url`
    // Here I'll make some get request with the url
  }
}

But I see the the router doesn't have the correct value yet, or at least, not all the time.

Funnily, sometime it does work.

What should I do to be able to get the location without using the window object?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

window.location.href defines DOM location, while router.url is based on basehref. Check basehref and RouterModule.forRoot([])

about 4 years ago · Juan Pablo Isaza Denunciar

0

Basically you use Angular Router to route between pages inside your application, and for every external route you should use window.location/href(html).

As answered here https://stackoverflow.com/a/72514835/12677894

window.location.href defines DOM location, while router.url is based on basehref *

about 4 years ago · Juan Pablo Isaza 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