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
				});
			}
		}
相关推荐
蓝冰凌20 分钟前
Vue 3 中 defineExpose 的行为【defineExpose暴露ref变量】详解:自动解包、响应性与实际使用
前端·javascript·vue.js
奔跑的呱呱牛33 分钟前
generate-route-vue基于文件系统的 Vue Router 动态路由生成工具
前端·javascript·vue.js
柳杉1 小时前
从动漫水面到赛博飞船:这位开发者的Three.js作品太惊艳了
前端·javascript·数据可视化
Greg_Zhong1 小时前
前端基础知识实践总结,每日更新一点...
前端·前端基础·每日学习归类
We་ct2 小时前
LeetCode 148. 排序链表:归并排序详解
前端·数据结构·算法·leetcode·链表·typescript·排序算法
IT_陈寒2 小时前
JavaScript开发者必看:5个让你的代码性能翻倍的隐藏技巧
前端·人工智能·后端
还是大剑师兰特2 小时前
Vue3 中 computed(计算属性)完整使用指南
前端·javascript·vue.js
井川不擦3 小时前
前端安全通信方案:RSA + AES 混合加密
前端
孜孜不倦不忘初心3 小时前
Ant Design Vue 表格组件空数据统一处理 踩坑
前端·vue.js·ant design