您的位置:首页 > 财经 > 产业 > 解决Vue2移动端(H5)项目,手机打开项目侧滑或者按物理返回键,始终是走this.$router.go(-1)

解决Vue2移动端(H5)项目,手机打开项目侧滑或者按物理返回键,始终是走this.$router.go(-1)

2024/10/6 12:31:09 来源:https://blog.csdn.net/cwin8951/article/details/141257448  浏览:    关键词:解决Vue2移动端(H5)项目,手机打开项目侧滑或者按物理返回键,始终是走this.$router.go(-1)

一、原因前言

最近开发Vue2移动端(H5)项目,用手机打开项目侧滑或者按物理返回键,始终是走this.$router.go(-1),即相当于点击了浏览器的返回键的项目。目前想要的效果是:只要回到初始页面,点击返回或者侧滑都应该回到工作台,不应该在一步一步的返回到之前进过的页面。

二、解决办法

需要监听HTML5``window.historypushState事件来解决

<template><div class="end_judging_scrap_steel"><t-header title="废钢终判" :back="goBack"><template #right><div @click="goToHistory" class="history">历史记录</div></template></t-header></div>
</template>
<script>
export default {name: 'endJudgingScrapSteel',data() {return {}},beforeDestroy() {window.removeEventListener('popstate', this.goBack, false)},mounted() {if (window.history && window.history.pushState) {// 向历史记录中插入了当前页history.pushState(null, null, location.href)window.addEventListener('popstate', this.goBack, false)}},methods: {goBack() {this.$router.push({ path: '/' });}},
};
</script>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com