共计 406 个字符,预计需要花费 2 分钟才能阅读完成。
在 vue 中,可以通过监听浏览器的 beforeunload 事件在关闭页面时执行你想要的操作。
具体代码如下
created() {
// 监听 beforeunload 事件
window.addEventListener('beforeunload', this.handleBeforeUnload);
},
destroyed() {
// 组件销毁前移除事件监听
window.removeEventListener('beforeunload', this.handleBeforeUnload);
//clearInterval(this.time);
},
methods: {handleBeforeUnload(event) { // 页面关闭
// 系统默认弹出框
event.returnValue = '你确定要离开吗?';
// 在这里执行你需要的操作,比如保存数据
this.doneLearning();},
原文地址: vue 关闭页面时执行事件
正文完