本文实例讲述了微信小程序获取网络类型的方法。分享给大家供大家参考,具体如下:
这里主要演示通过wx.getNetworkType获取当前网络类型的操作方法。代码如下:
index.js:
Page({
data: {
netType:''
},
onLoad: function () {
var that = this;
try {
wx.getNetworkType({
success: function(res) {
that.setData({
netType:res.networkType
})
},
})
} catch (e) {
// Do something when catch error
}
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
}
})
index.wxml:
网络类型 {{netType}}
index.wxss:
.table {
border: 0px solid darkgray;
}
.tr {
display: flex;
width: 100%;
justify-content: center;
height: 2.6rem;
align-items: center;
}
.td {
width:40%;
justify-content: center;
text-align: center;
}
.bg-g{
background: #E6F3F9;
}
运行结果:
希望本文所述对大家微信小程序开发有所帮助。



