小程序获取网络类型代码

小程序获取网络类型代码!本文实例讲述了成都微信小程序开发获取网络类型的方法。分享给大家供大家参考,具体如下:

这里主要演示通过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:

<view class="table">
<view class="tr bg-g">
<view class="td">网络类型</view>
<view class="td">{{netType}}</view>
</view>
</view>
index.wxss:

/**index.wxss**/
.table {
border: 0px solid darkgray;
}
.tr {
display: flex;
100%;
justify-content: center;
height: 2.6rem;
align-items: center;
}
.td {
40%;
justify-content: center;
text-align: center;
}
.bg-g{
background: #E6F3F9;
}

原文地址:https://www.cnblogs.com/hsccxt/p/10457850.html