h5页面的头部返回箭头或者手势返回到指定的页面

解决这个问题我们需要用到window.history的pushState方法,不了解这个可以去百度了解一下:(下面粘贴上我百度的对他的解释)
window.history.pushState(data, title, targetURL);
@状态对象:传给目标路由的信息,可为空
@页面标题:目前所有浏览器都不支持,填空字符串即可
@可选url:目标url,不会检查url是否存在,且不能跨域。如不传该项,即给当前url添加data。
1.我的h5页面时使用webview嵌套在小程序中的,我想在打开的多个h5页面中,点击返回页面顶部的返回箭头,或者手机的手机返回,直接返回到小程序的首页,此时注意的是,webview嵌套h5页面的头部并不是小程序的,是我们h5页面的,所以我们要在h5这个需要跳转到小程序的页面,加入如下代码:

import wx from "weixin-js-sdk";
mounted() {
      if (window.history && window.history.pushState) {
        history.pushState(null, null, document.URL);
        window.addEventListener("popstate", this.goBack, false);
      }
  },
 destroyed() {
      window.removeEventListener("popstate", this.goBack, false);
  },
 methods: {
   goBack() {
          wx.miniProgram.switchTab({ url: "/pages/index/index" });
      }
    },

我们不再微信小程序里面嵌套,直接在h5页面上也是可以直接使用的:

mounted() {
      if (window.history && window.history.pushState) {
        history.pushState(null, null, document.URL);
        window.addEventListener("popstate", this.goBack, false);
      }
  },
 destroyed() {
      window.removeEventListener("popstate", this.goBack, false);
  },
 methods: {
   goBack() {
         this.$router.replace({path:'/'}); //退回到h5首页,避免返回来回死循环跳转,建议使用this.$router.replace(),跳转到指定url路径,但是history栈中不会有记录,点击返回不会跳转到上个页面。
      }
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值