I have a scenario where I am checking if the browser tab is closed using the code below. It currently works with windows dialog, but I would like to incorporate MatDialog for confirmation instead.
@HostListener('window:beforeunload', ['$event'])
beforeunloadHandler(event) {
return false;
}
I attempted the following code, however, it did not work as expected.
@HostListener('window:beforeunload', ['$event'])
beforeunloadHandler(event) {
this.iDialogData.title = 'Unsaved changes';
this.iDialogData.bodyMessage = 'If you leave this page, any unsaved changes will be lost.';
this.iDialogData.cancelBtnText = 'Cancel';
this.iDialogData.mainbtnText = 'Leave page';
const dialogRef = this.openDialog();
return dialogRef.afterClosed().pipe(map(result => {
if (result !== undefined)
return true;
else
return false;
}));
}