Angular 2窗口.在关闭窗口时,beforeunload显示Primeng vensigndialog不起作用



请帮助如何在关闭选项卡/窗口或离开页面

时保留对话框
export class AppComponent{constructor(private confirmationService:ConfirmationService){}@HostListener('window:beforeunload', ['$event'])    public onWindowScroll( $event:any){      console.log('scrolled');
      return new Promise((observer) => {this.confirmationService.confirm({
           message:"All the information would be lost, are you sure you want to move out of the screen?", accept: () => { $event.returnValue ="";
              observer(true);
            },
            reject: () => {
                observer(false);
            }
        });
    });
    }
}
ngOnInit() {       
        const unloadEvent = (e) => {
          if (**YOUR CHECK FLAG**) {
            const confirmationMessage =
              'Warning: Leaving this page will result in any unsaved data being lost. Are you sure you wish to continue?';
            (e || window.event).returnValue = confirmationMessage; // Gecko + IE
            return confirmationMessage; // Webkit, Safari, Chrome etc.
          }
        };
        window.addEventListener('beforeunload', unloadEvent);
        window.onunload = () => {
         // Post Unload operations
            }
          });
        };
      }

最新更新