1.在app.js的onLaunch中添加如下方法:
App({
onLaunch: function () {
var that = this;
wx.onNetworkStatusChange(function (res) {
if (res.networkType == 'none') {
that.globalData.nonetwork = true;
} else {
that.globalData.nonetwork = false;
}
})
},
globalData: {
nonetwork: false,
}
})
2.在index.wxml页面监听当前网络状态
.js
var app = getApp();
page({
data: {
nonetwork: false,
},
onShow: function () {
var that = this;
that.setData({
nonetwork: app.globalData.nonetwork,
})
},
})
.wxml
< block wx: if= "{{nonetwork}}" >
<view>当前网络不可用,请检查您的网络</view>
</block >
api文档: https://developers.weixin.qq.com/miniprogram/dev/api/device/network/wx.onNetworkStatusChange.html
来源:博客园
作者:pycmsj
链接:https://www.cnblogs.com/pycmsj/p/11739542.html