Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda