项目需要监听浏览器的返回事件,然后我写了以后发现这个页面需要点击两次返回才能回去,请教这是什么原因,代码是vue的。
methods:{
//点击返回时执行的方法
backTo(){
}
},
mounted () {
if (window.history && window.history.pushState) {
history.pushState(null, null, document.URL)
window.addEventListener('popstate', this.backto)
}
},
destroyed () {
window.removeEventListener('popstate', this.backto)
}
代码如上,点击返回时能执行backto,但是要点两次才能跳过去
因为你调了history.pushState啊