In my Angular application, I have implemented a @HostListener
that triggers when the back button on the browser is clicked. However, I have noticed that the event handler seems to be firing twice - prompting two dialogue boxes when clicking 'Ok'. This leads me to believe that there may be a bug causing this behavior.
@HostListener('window:popstate', ['$event'])
onBrowserBackBtnClose(event: Event) {
// If we can go back...
if (this.canGoBack) {
alert('Are you sure you want to go back and exit the review?');
}
}
I am wondering if this issue is specific to Angular or if it is a normal behavior related to routing mechanisms. Additionally, I would like to know if there is a way to fix this problem or implement a workaround. Perhaps there is a more effective way in Angular to handle actions triggered by the back button click?