vue 監聽網頁關閉/瀏覽器關閉事件
阿新 • • 發佈:2021-01-07
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))
}