目录如何清除浏览器历史栈问题Vue返回首页后如何清空路由需求一:从首页点击路由到A页面需求二:把浏览器的记录返回指定的页面如何清除浏览器历史栈 问题 需要跳转好几个页面进行表单提交,
需要跳转好几个页面进行表单提交,提交完之后,跳转回首页,返回上一页,发现还可以返回上一级页面路由
//可以拿到历史记录栈,清空栈
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);
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: vue如何清除浏览器历史栈
本文链接: https://www.lsjlt.com/news/149665.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0