項目中有一個微信掃碼開箱的功能,在存在多個包裹的情況下,在微信瀏覽器點擊返回按鈕時會出現再次開箱的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阻止默認事件
}
如有不對,請各位指出,謝謝