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 天前
Vue生命周期
前端·javascript·vue.js
每天吃饭的羊1 天前
UMD和IIfe
开发语言·前端·javascript
前端那点事1 天前
Vue线上代码调试全攻略(安全无侵入,新手也能上手)
前端·vue.js
前端那点事1 天前
Vue批量文件上传并发踩坑指南:3步解决阻塞、限流、进度混乱
前端·面试
桔筐1 天前
Vue3 v-model 双向绑定导致循环触发的坑
前端·javascript·vue.js
Alice-YUE1 天前
前端图片优化完全指南:从格式到加载的全面提速方案
前端·笔记·学习
fen_fen1 天前
下载Chrome浏览器对应的Driver
前端·chrome
路光.1 天前
ReferenceError:Can‘t find variable:structureClone
前端·javascript·html·vue2
前端那点事1 天前
内存泄漏排查全指南:从场景识别到工具实操,新手也能上手
前端·vue.js
我这一生如履薄冰~1 天前
浏览器多窗口同开一页面,数据同步更新(纯前端方案)
前端·javascript