//獲取當(dāng)前網(wǎng)絡(luò)狀態(tài)
GetCurrentNetwork() {
var that = this
//獲取網(wǎng)絡(luò)類型
wx.getNetworkType({
success: function(res) {
const networkType = res.networkType
//不為none代表有網(wǎng)絡(luò)
if ('none' != networkType) {
that.globalData.hasnetword = true
wx.showToast({
icon: 'none',
title: '當(dāng)前為' + res.networkType + '網(wǎng)絡(luò)'
})
} else {
wx.showToast({
icon: 'none',
title: '無網(wǎng)絡(luò),請檢查網(wǎng)絡(luò)連接'
})
}
},
})
},
//監(jiān)聽網(wǎng)絡(luò)變化
onNetWorkChange(){
wx.onNetworkStatusChange(function(res) {
console.log(res)
if (res.isConnected) {
that.globalData.hasnetword = true
wx.showToast({
icon: 'none',
title: '當(dāng)前為' + res.networkType + '網(wǎng)絡(luò)'
})
} else {
that.globalData.hasnetword = false
wx.showToast({
icon: 'none',
title: '無網(wǎng)絡(luò),請檢查網(wǎng)絡(luò)連接'
})
}
})
}
在onLoad 或 onShow 中使用這兩個(gè)函數(shù),小程序開發(fā)工具中可以獲取到當(dāng)前網(wǎng)絡(luò)狀態(tài)耙饰,當(dāng)前關(guān)閉網(wǎng)絡(luò)時(shí)監(jiān)測網(wǎng)絡(luò)API沒觸發(fā)不知道怎么回事纹笼?