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;
  }
}
5
  • Does this answer your question? How to redirect to an external URL from angular2 route without using component? Commented Aug 20, 2020 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. Commented Aug 20, 2020 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 Commented Aug 20, 2020 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? Commented Aug 20, 2020 at 17:45
  • 1
    Exactly, not with any builtin Angular component at least. Commented Aug 20, 2020 at 17:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.