参考博文:vuejs中监听窗口关闭和窗口刷新事件的方法

1.methods中定义窗口关闭/浏览器关闭之后处理内容,例如删除cookie、session、local storage 等

methods: {
    beforeunloadHandler(e) {
      console.log('关闭窗口之后')
    }
}

2.mounted 绑定窗口关闭监听事件

mounted() {
    window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
}

3.destroyed 解绑事件

destroyed() {
    window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))
}

以上方法刷新页面也会触发

仅监听浏览器关闭事件,参考博客:Vue中监听窗口关闭事件,并在窗口关闭前发送请求

Logo

前往低代码交流专区

更多推荐