本文實例講述了微信小程序獲取網絡類型的方法。分享給大家供大家參考,具體如下:
這里主要演示通過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; width: 100%; justify-content: center; height: 2.6rem; align-items: center;}.td { width:40%; justify-content: center; text-align: center;}.bg-g{ background: #E6F3F9;}
運行結果:
希望本文所述對大家微信小程序開發有所幫助。
新聞熱點
疑難解答