我想知道如何實現確認退出組件,以便在頁面刷新或離開選項卡或窗口或屏幕上我可以執行退出方法確認,以便如果用戶單擊OK
他將離開屏幕和點擊NO
他會保持在同一屏幕上?我在這裏使用如何實現在角度4退出確認?
代碼是:
import { Component, OnInit, Input, Output, HostListener } from '@angular/core';
@Component({
selector: 'confirmonexit',
templateUrl: './confirm-on-exit.html'
})
export class ConfirmOnExitComponent {
@Input() isDirty: boolean;
public isConfirmed: boolean = false;
@HostListener('window:beforeunload',['$event']) beforeUnloadHander() {
if (this.isDirty) {
let msg: string = 'Are you sure you want to navigate without saving the entered data on the screen ? '
if (confirm(msg)) {
this.isDirty = false;
this.isConfirmed = false;
} else {
this.isDirty = true;
event.preventDefault();
}
}
}
}
appModule added - this component in declarations
html added = <confirmonexit [isDirty]="CreateEditForm.form.dirty"></confirmonexit>
我不知道在哪裏的錯誤是。我無法執行此功能。任何人都可以幫我嗎?
我們不能使用這個現有的代碼?我的意思是@HostListener('window:beforeunload',['$ event'])。 –
任何想法??想要幫助。 –
您提到的方法只在用戶離開窗口時才起作用,但如果用戶導航到其他選項卡,則該方法將不會執行,因爲您正在單個頁面應用程序上工作,您只有一個窗口 –