项目中有一个微信扫码开箱的功能,在存在多个包裹的情况下,在微信浏览器点击返回按钮时会出现再次开箱的bug,以下方法为主要争对此bug的处理
methods: {
pushHistory() {
var state = {
title: this.$route.meta.title,
url: this.$route.path
}
window.history.pushState(state, "title", "#")
},
// 检查微信环境
weixinClosePage() {
if (typeof WeixinJSBridge == "undefined") {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', weixin_ClosePage, false)
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', weixin_ClosePage)
document.attachEvent('onWeixinJSBridgeReady', weixin_ClosePage)
}
} else {
weixin_ClosePage()
}
},
// 关闭微信浏览器
weixin_ClosePage() {
WeixinJSBridge.call('closeWindow')
}
},
created() {
this.pushHistory()
window.addEventListener("popstate", function(e) {
weixinClosePage()
}, false)
},
beforeDestroy() {
window.removeEventListener('popstate', false) // false阻止默认事件
}
如有不对,请各位指出,谢谢