Angular 2 Window.onbeforeunload显示primeng ConfirmDialog在关闭窗口时不起作用

时间:2017-12-14 14:01:16

标签: angular angular2-routing primeng

请关闭关闭标签/窗口或离开页面

时如何保留对话框
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);
            }
        });
    });
    }
}

1 个答案:

答案 0 :(得分:0)

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
            }
          });
        };
      }