vue監聽頁面離開事件
var app = new Vue({
el: '#app',
data() {
},
mounted() {
document.addEventListener('visibilitychange', this.handleVisiable)
},
destroyed() {
document.removeEventListener('visibilitychange', this.handleVisiable)
},
methods: {
handleVisiable(e) { // 監聽頁面離開事件
if (e.target.visibilityState === 'visible') { //返回頁面
window.location.href = 'http://www.baidu.com'
}
}
}
})
