Oussama EL
Oussama EL

Reputation: 154

Angular 8 - navigate to a component then refresh window

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.

Upvotes: 2

Views: 181

Answers (1)

SquarePeg
SquarePeg

Reputation: 1781

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();
});

Upvotes: 1

Related Questions