vue框架实现清除浏览器历史栈的做法是什么
Admin 2022-09-05 群英技术资讯 1179 次浏览
需要跳转好几个页面进行表单提交,提交完之后,跳转回首页,返回上一页,发现还可以返回上一级页面路由
//可以拿到历史记录栈,清空栈
let routeHistory=history.length-1;
this.$router.go(-routeHistory);
那么问题来了
点击返回首页后,再点击手机的返回键 会打开D页面 再按手机返回键 会打开C页面,依次类推,
如何才能实现点击返回首页后,清空路由呢
mounted () {
if (window.history && window.history.pushState) {
// 向历史记录中插入了当前页
history.pushState(null, null, document.URL);
window.addEventListener('popstate', this.goBack, false);
}
},
destroyed () {
window.removeEventListener('popstate', this.goBack, false);
},
methods: {
goBack () {
// console.log("点击了浏览器的返回按钮");
sessionStorage.clear();
window.history.back();
},
}
禁止有返回记录
mounted () {
if (window.history && window.history.pushState) {
// 向历史记录中插入了当前页
history.pushState(null, null, document.URL);
window.addEventListener('popstate', this.goBack, false);
}
},
destroyed () {
window.removeEventListener('popstate', this.goBack, false);
},
methods: {
goBack () {
// console.log("点击了浏览器的返回按钮");
history.pushState(null, null, document.URL);
},
}
mounted 中:
if (window.history && window.history.pushState) {
history.pushState(null, null, document.URL);
window.addEventListener("popstate", _this.onClickLeft, false); //_this.onClickLeft是返回的点击事件
}
methods: {
onClickLeft() {
// this.$route.query.radio支付页面到指定页面传的参数 来判断他的路由
if (this.$route.query.radio == 1 || this.$route.query.radio == 2) {
this.$router.push({ //返回指定页面
});
} else {
this.$router.go(-1); // 正常返回
}
},
// 将事件清除掉
destroyed() {
window.removeEventListener("popstate", this.onClickLeft, false);
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本文实例为大家分享了vue实现拖拽交换位置的具体代码,供大家参考,具体内容如下template div class=root transition-group tag=div class=container div class=item :class=item + i
这篇文章主要给大家介绍了关于vue.js watch经常失效的场景与解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章我们来了解React组件通信的内容,本文会介绍组件通信是什么,如何通信,及示例等等,对大家学习和理解React组件通信有一定的帮助,感兴趣的朋友可以参考。
ajax和websocket有哪些不同?对于ajax和websocket的区别,一些朋友比较好奇,其中两者在本质、生命周期、适用范围、发起人和用法这五方面都有区别,接下来我们详细的了解看看。
怎样用React实现有动态效果的弹窗组件?对于实现一个简单的弹窗组件还是比较简单的,但是本文给大家分享的带有动态效果的,也就是在弹出弹窗的时候有动效展示。下面我们就来看看怎样实现。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008