0

I have angular application in which I need to reload page after navigation. I have applied this solution but window.location.reload(); does not work on production. So I have added useHash: true in app-routing.module.ts file. But that also giving me problems while applying SSR in application. So I need alternate way for below code:

this.router.navigate(['path/to'])
  .then(() => {
    window.location.reload();
  });

I have tried by applying window.location.href = window.location.href , window.location.href = window.location.protocol + '//' + window.location.host + '/path/to'; instead of window.location.reload(); but it does not reload the page as expected.

Is there any other way to achieve reloading after navigating in angular 12? Please help and guide.

Edit : Why reload: In application, after login of user, notifications are shown in header. That does not show data if page not reloaded.

ganesh
  • 416
  • 1
  • 11
  • 32
  • Why do you need a reload? – MaartenDev Jan 18 '22 at 18:25
  • It looks like you want an anchor tag behavior written in JavaScript. Am I right? – Chris Claude Jan 18 '22 at 18:40
  • I think you are solving the wrong problem. If you want to refresh your login notifcations why not use a subscription or state solution like redux to update state from any placve? – MaartenDev Jan 18 '22 at 19:08
  • @MaartenDev pls check Edit in question, for your question – ganesh Jan 18 '22 at 19:08
  • @MaartenDev I am beginner to angular, not aware of solution you are suggesting. could you please give some example of your solution for angular? pls guide. – ganesh Jan 18 '22 at 19:11
  • You could look into "Sharing data between angular components", you could checkout: https://fireship.io/lessons/sharing-data-between-angular-components-four-methods/ – MaartenDev Jan 18 '22 at 19:16

1 Answers1

1

You could try to redirect to root and then redirect to your final destination:

this.router.navigate(['/'])
  .then(() => {
    this.router.navigate(['path/to'])
  });
MaartenDev
  • 5,631
  • 5
  • 21
  • 33