uniapp:H5端reLaunch跳转后,返回还有页面存在问题

1.跳转后页面还存在的原因

  • H5端调用uni.reLaunch之后之前页面栈会销毁,但是无法清空浏览器之前的历史记录,此时navigateBack不能返回,如果存在历史记录的话点击浏览器的返回按钮或者调用history.back()仍然可以导航到浏览器的其他历史记录。

2.跳转后,在跳转后的页面里,加载方法

javascript 复制代码
onLoad(option) {
  // #ifdef H5
  this.initPreventBack();
  // #endif
},
javascript 复制代码
methods: {
		
			initPreventBack() {
				// 禁用浏览器返回按钮
				history.pushState(null, null, document.URL);
				window.addEventListener('popstate', this.handlePopState);

				// 禁用手势返回(左右边缘都监听)
				document.addEventListener('touchstart', this.handleTouchStart, {
					passive: false
				});
				document.addEventListener('touchmove', this.handleTouchMove, {
					passive: false
				});
			},

			removePreventBack() {
				window.removeEventListener('popstate', this.handlePopState);
				document.removeEventListener('touchstart', this.handleTouchStart);
				document.removeEventListener('touchmove', this.handleTouchMove);
			},

			handlePopState() {
				history.pushState(null, null, document.URL);
				this.showPreventToast();
			},

			handleTouchStart(e) {
				// 记录触摸起始位置
				this.startX = e.touches[0].clientX;
			},

			handleTouchMove(e) {
				if (!this.startX) return;

				const currentX = e.touches[0].clientX;
				const screenWidth = window.innerWidth;

				// 检测边缘滑动(左右各10px范围内)
				const isEdgeSwipe = this.startX < 10 || this.startX > screenWidth - 10;

				// 检测滑动方向(防止误触)
				const isBackSwipe = Math.abs(currentX - this.startX) > 30;

				if (isEdgeSwipe && isBackSwipe) {
					e.preventDefault();
					this.showPreventToast();
				}
			},

			showPreventToast() {
				uni.showToast({
					title: '跳转完成,退出程序!',
					icon: 'none',
					duration: 2000
				});
			}
		}
相关推荐
AI前端老薛7 分钟前
CSS实现动画的几种方式
前端·css
晨米酱9 分钟前
轻量级 Git Hooks 管理工具 Husky
前端·代码规范
携欢11 分钟前
portswigger靶场之修改序列化数据类型通关秘籍
android·前端·网络·安全
GuMoYu12 分钟前
npm link 测试本地依赖完整指南
前端·npm
代码老祖13 分钟前
vue3 vue-pdf-embed实现pdf自定义分页+关键词高亮
前端·javascript
未等与你踏清风13 分钟前
Elpis npm 包抽离总结
前端·javascript
代码猎人13 分钟前
如何使用for...of遍历对象
前端
秋天的一阵风15 分钟前
🎥解决前端 “复现难”:rrweb 录制回放从入门到精通(下)
前端·开源·全栈
林恒smileZAZ15 分钟前
【Vue3】我用 Vue 封装了个 ECharts Hooks
前端·vue.js·echarts
颜酱16 分钟前
用填充表格法-继续吃透完全背包及其变形
前端·后端·算法