0

I have been stuck in this for a long time. How can I replace window.location.href = url to use Angular routing?

checkforStoredLocale() {
  if (this.storedlocale && this.storedlocale !== this.localeId) {
    this.consoleService.consoleMessage('redirecting');
    let port = this.location.port.length > 0 ? `:${this.location.port}` : '';
    let url = `${this.location.protocol}//${this.location.hostname}${port}${this.baseHref}${this.storedlocale}${this.location.pathname}`;
    this.consoleService.consoleMessage(url);
    // this.route.navigateByUrl(url);
    window.location.href = url;
  }
}
R. Richards
  • 24,603
  • 10
  • 64
  • 64
Emmon
  • 377
  • 1
  • 9
  • 24
  • Does this answer your question? [How to redirect to an external URL from angular2 route without using component?](https://stackoverflow.com/questions/40150393/how-to-redirect-to-an-external-url-from-angular2-route-without-using-component) – Marc Sances Aug 20 '20 at 17:38
  • 3
    Angular Router is reserved for in app routing, you need to go old school js (as you already did) for location changes. – Berk Kurkcuoglu Aug 20 '20 at 17:40
  • @MarcSances no it doesn't if anything your suggested link also uses the window.location.href, which i don't want to – Emmon Aug 20 '20 at 17:43
  • @BerkKurkcuoglu do you mean to say there is no way I could redirect to the said url without using that window.location.href please? – Emmon Aug 20 '20 at 17:45
  • 1
    Exactly, not with any builtin Angular component at least. – Berk Kurkcuoglu Aug 20 '20 at 17:47

0 Answers0