vue项目监听浏览器的刷新和关闭按钮

vue项目监听浏览器的刷新和关闭按钮_第1张图片

mounted() {

window.addEventListener('beforeunload', (e) => this.beforeunloadHandler(e));

window.addEventListener('unload', this.updateHandler);

},

methods: {

beforeunloadHandler(e) {

e = e || window.event;

if (e) {

e.returnValue = '关闭提示';

}

return '关闭提示';

},

updateHandler() {

fetch('url', {

method: 'POST',

body:'参数',

headers: {'Content-Type': 'application/json'},

keepalive: true

});

}

},

你可能感兴趣的:(vue.js,javascript,前端)