解决这个问题我们需要用到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栈中不会有记录,点击返回不会跳转到上个页面。
}
},