最近公眾號(hào)項(xiàng)目開(kāi)發(fā)中,有一個(gè)解綁需求,要求解綁之后,關(guān)閉微信瀏覽器。因?yàn)槭俏⑿艃?nèi)置瀏覽器,用不了 window 對(duì)象,所以有 2 種關(guān)閉方式。
一、WeixinJSBridge 對(duì)象
WeixinJSBridge.call('closeWindow');
二、公眾號(hào)開(kāi)發(fā)比較適用的方法
wx.closeWindow();
這個(gè)方法必須引入 JS-SDK ,如果不是公眾號(hào)項(xiàng)目的話,建議使用第一種方法。
下面附上完整代碼
index.html 文件中引入
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js "></script>
在需要關(guān)閉微信瀏覽器的地方直接使用此方法,我這里是解綁之后關(guān)閉,所以寫(xiě)在了接口里
close(){
this.$api.user.unBing().then(res => {
if (res.data.code === 200) {
this.$toast('解綁成功')
wx.closeWindow()
} else {
this.$toast(res.data.msg)
}
})
}