写vue3的H5项目的时候有个需求是回退时弹窗确认是否退出当前页面

一、第一个办法------使用onbeforeRouteLeave路由钩子

 const formRouteAbi = localStorage.getItem("formRouteAbi"); //获取上一页路径
onBeforeRouteLeave((to, from, next) => {
if (formRouteAbi == to.fullPath && topicInfo.topicNow > 0) { Dialog.confirm({
            closeOnPopstate: false,//不加这个弹窗不会展示的,因为路由发生了变化
          message: `你确定要退出社交能力测试吗,再测试${
topicInfo.topicNum - topicInfo.topicNow
}个题就完成该能力测试了。`,
confirmButtonText: "继续测试",
cancelButtonText: "确认退出",
})
.then(() => {
next(false);
})
.catch(() => {
next(true);
});
} else {
next(true);
}
});

二、第二个办法------使用history.pushState(null, "null", document.URL);禁用前进后退功能

禁用回退功能,手动添加跳转事件

onMounted(() => {
history.pushState(null, "null", document.URL);
window.addEventListener("popstate", handleEvent, false);
});
onUnmounted(() => {
window.removeEventListener("popstate", handleEvent, false);
});

const url =
process.env.NODE_ENV == "production"
? location.origin + "/shengchan"
: location.origin;
const handleEvent = () => {
history.pushState(null, "null", document.URL);
if (topicInfo.topicNow > 0) {
Dialog.confirm({
closeOnPopstate: false,
message: `你确定要退出社交能力测试吗,再测试${
topicInfo.topicNum - topicInfo.topicNow
}个题就完成该能力测试了。`,
confirmButtonText: "继续测试",
cancelButtonText: "确认退出",
})
.then(() => {})
.catch(() => {
location.href = `${url}${formRouteAbi}`;
});
} else {
location.href = `${url}${formRouteAbi}`;
}
};

两种方法,都可以实现

最新文章

  1. 【Swift学习】Swift编程之旅---方法(十五)
  2. C#项目””是针对”.NETFramework,Version=v4.5.2”但此计算机没有,需要修改为v4.5.1.
  3. snakeyaml - Documentation.wiki
  4. 什么是智能扫描开单打印进销存POS?安卓工业手持PDA设备上的POS销售开单,现场打印打票,用扫描枪太方便了
  5. Kafka安装及部署
  6. Gabor学习笔记
  7. 【FE前端学习】第二阶段任务-基础
  8. 使用Spring的注解方式实现AOP
  9. Eclipse c++环境搭建 并加载OpenCV库 2015最新
  10. 去掉display:inline-block元素间的多余空白
  11. a标签的背景图在ie8下显示问题
  12. haslayout和BFC
  13. android开发
  14. springmvc+mybatis+redis(转)
  15. 求第i个小的元素 时间复杂度O(n)
  16. 5、通过js代码设置css样式
  17. Jquery ajaxSubmit()的浏览器兼容问题
  18. AVFoundation 框架初探究(三)
  19. EBS预置文件作用收集整理
  20. phpstudy配置本地https

热门文章

  1. Python语法与基本数据类型
  2. Objects非空判断-声明异常throws
  3. ASCLL编码器-算术运算符_四则与取模运算
  4. 继承QAbstractTableModel QStyledItemDelegate实现自定义表格,添加进度条和选中框。
  5. immutable.js学习笔记(一)----- immutable.js 简介
  6. xml基本学习
  7. P3_注册小程序账号&安装开发者工具
  8. react中redux怎么使用
  9. CCRD_TOC_2008年第3期
  10. 【javascript】slice()、substring()和substr() 三种字符串截取方法区别