Back button event does not fire Angular

Home / Uncategorized / Back button event does not fire Angular

Question:
I am trying to redirect the user to a particular url on the click of back button in browser, and here is the code
constructor(private router: Router,private location: PlatformLocation) {
let eventUrl = window.sessionStorage.getItem(‘Event’);
this.location.onPopState(() => {
alert(‘click’);
this.router.navigate([‘/’, this.selectedLanguage, ‘event’, eventUrl]);
});
}

but it does not seem to fire the event when user click on back button. The code is in the current component. what is the issue?


Answer:
You can bind things you want to do while leaving the app to window.unload event. Here I used the HostListener.
@HostListener(‘window: unload’, [‘$event’])
unload(event) {
window.open(‘https://localhost:4200’, ‘_blank’);
window.close();
}

Mention that Chrome will block window.open by default, you have to change block policy.
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *