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
				});
			}
		}
相关推荐
于慨18 小时前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
石小石Orz18 小时前
油猴脚本实现生产环境加载本地qiankun子应用
前端·架构
从前慢丶18 小时前
前端交互规范(Web 端)
前端
CHU72903518 小时前
便捷约玩,沉浸推理:线上剧本杀APP功能版块设计详解
前端·小程序
GISer_Jing19 小时前
Page-agent MCP结构
前端·人工智能
王霸天19 小时前
💥别再抄网上的Scale缩放代码了!50行源码教你写一个永不翻车的大屏适配
前端·vue.js·数据可视化
小领航19 小时前
用 Three.js + Vue 3 打造炫酷的 3D 行政地图可视化组件
前端·github
@大迁世界19 小时前
2026年React大洗牌:React Hooks 将迎来重大升级
前端·javascript·react.js·前端框架·ecmascript
PieroPc19 小时前
一个功能强大的 Web 端标签设计和打印工具,支持服务器端直接打印到局域网打印机。Fastapi + html
前端·html·fastapi
悟空瞎说19 小时前
深入 Vue3 响应式:为什么有的要加.value,有的不用?从设计到源码彻底讲透
前端·vue.js