mounted(){
if (window.history && window.history.pushState) {
// @ts-ignore
history.pushState(null, null, document.URL);
window.addEventListener('popstate', this.goBack, false);
}
}
methods:{
goBack(){
console.log('支持');
this.$router.replace({path: '/'});
//replace替换原路由,作用是避免回退死循环
}
}
destroyed() {
// 清除监听
window.removeEventListener('popstate', this.goBack, false);
}
本文详细介绍了在Vue中如何使用history.pushState方法配合监听popstate事件实现路由切换的监听及回退功能,通过代码示例展示了如何在组件挂载和销毁时正确地添加和清除事件监听器。
876

被折叠的 条评论
为什么被折叠?



