Angular 8 - navigate to a component then refresh window

Viewed 138

I want to navigate to a component then refresh the window when clicking on switch() button.

I have tried:

switch() {
    this.router.navigateByUrl("/layouts");
    window.location.reload();
  }

but it is not working as expected, it is only reloading the page without navigate.

1 Answers

navigateByUrl is asynchronous so window.location.reload() is getting called before it returns causing the page to reload before navigating.

navigateByUrl returns a promise so you can do:

this.router.navigateByUrl("/layouts").then(() => {
    window.location.reload();
});
Related