In my Angular 7 app, I have a canDeactivate guard to alert user of unsaved changes. This guard also guard from leaving the page
@HostListener('window:beforeunload')
public canDeactivate(): boolean {
return this.contentChanged === false;
}
On the same page, I have some function to download from AWS S3
async downloadAttachment(url: string, e: any) {
const target = e.target || e.srcElement || e.currentTarget;
window.onbeforeunload = null;
if (!target.href) {
e.preventDefault();
target.href = await this.storageService.getDownloadLink(
url,
);
target.download = this.storageService.getFileName(url);
target.click();
}
}
The issue is when I have unsaved changes(contentChanged=true), the download will trigger the window:beforeunload event and the browser will alert
and user has to click "Leave" to download the file. The downloading process does not leave the page actually.
I tried to add "window.onbeforeunload = null" in the code, but it does not work in my code.
How to allow user to download without seeing the meaningless alert?