vue中在mounted中使用了两个监听 window.addEventListener('popstate',.....)其中的方法不一样 但是不生效;单独使用的话是生效的
这个问题怎么解决呀 求解
methods:{
goBack() {
window.history.back()
history.pushState(null, null, document.URL)
},
},
mounted(){
// 第一个监听addEventListener
var that = this
let name = that.$route.query.name
history.pushState(null, null, document.URL)
window.addEventListener(
'popstate',
(e) => {
e.preventDefault()
history.pushState(null, null, document.URL)
that.$router.push({
path: '/',
query: {
name,
},
})
// console.log(mode)
// console.log("我监听到了浏览器的返回按钮事件啦");//根据自己的需求实现自己的功能
},
false
)
//第二个监听 addEventListener
if (window.history && history.pushState) {
history.pushState(null, null, document.URL)
window.addEventListener('popstate', this.goBack, false)
}
}