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
				});
			}
		}
相关推荐
回眸&啤酒鸭1 小时前
【回眸】OpenSwarm 多智能体协作系统实战指南
大数据·前端·人工智能
用户69371750013841 小时前
2026,程序员的时代拐点到了
android·前端·后端
大龄秃头程序员1 小时前
一次 iBeacon + BLE 无感解锁方案的实现记录
前端
小兔子1 小时前
Python 的 GIL 与 free-threading:3.13 之后「去 GIL」走到哪一步了
前端
IT_陈寒2 小时前
SpringBoot自动配置差点让我加班到凌晨
前端·人工智能·后端
guslegend3 小时前
脚手架原理与本地调试:从 bin 软链接到 npm link
前端·npm·node.js·脚手架·前端工程化
海码事务所3 小时前
Google Play 新个人开发者账号上架指南:12 人连续 14 天封闭测试怎么做?
前端
田威AI3 小时前
图片内文字翻译的规格:输入输出、保真、自动化、时间与费用
前端·计算机视觉
不可能片场3 小时前
命令行中文变问号 我用环境变量救了场
前端·electron
lerhxx4 小时前
AI 应用中的上下文管理 —— 从"上下文窗口"到"分层压缩"的工程实践
前端·javascript