APP.vue中定义方法
beforeunloadHandler(e) { //if (this.$route.name !== "login" && !isHomePage && invalid != 0) { // this.beforeUnloadTime = new Date().getTime(); // e = e || window.event; //if (e) { // e.returnValue = "关闭提示"; //} //return "关闭提示"; //} }
mounted中监听 beforeunload 事件
window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
destroyed中卸载事件
window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))