Search code examples
angularangular-materialangular-reactive-formsrouter

Angular routing is not working with hostlistener/location popstate event(browser back event)


Angular routing is not working under hostlistener event. When user click on back button I want to navigate on specific page so I have used popstate hostlistener event but navigation is not happening for me.

Can someone help me please?

@HostListener('window:popstate')
  onPopState() {
    this.router.navigate(['user-list']);
  }
}

Solution

  • I found a workaround of setTimeout which works fine for me.

    @HostListener('window:popstate')
      onPopState() {
          setTimeout(() => {
              this.router.navigate(['user-list']);
          });
      }
    }