Reputation: 1100
I am using HostListener to get window close event and try to prevent the event.
I imported HostListener First and Used this code. But, I couldn't prevent the window close event.
import { HostListener } from '@angular/core';
@HostListener('window:beforeunload', ['$event'])
onWindowClose(event: any): void {
this.doSomething();
}
Upvotes: 3
Views: 10600
Reputation: 1100
You can prevent window close event for separate component, using HostListener.
First Import HostListener in your component
import { HostListener } from '@angular/core';
And, Use this code to prevent window close.
@HostListener('window:beforeunload', ['$event'])
onWindowClose(event: any): void {
// Do something
event.preventDefault();
event.returnValue = false;
}
Now, Your event was prevented.
Upvotes: 4