uni-app 判断网络

获取网络类型

方法一

uni.getNetworkType({
    success: function (res) {
        console.log(res.networkType);//网络类型 wifi、2g、3g、4g、ethernet、unknown、none
        if(res.networkType === "none"){
            console.log("当前无网络");
        }else{
            console.log("有网络");
        }
    }
});

方法二

getNetworkType() {
    var types = {};
    types[plus.networkinfo.CONNECTION_UNKNOW] = "Unknown";
    types[plus.networkinfo.CONNECTION_NONE] = "None";
    types[plus.networkinfo.CONNECTION_ETHERNET] = "Ethernet";
    types[plus.networkinfo.CONNECTION_WIFI] = "WiFi";
    types[plus.networkinfo.CONNECTION_CELL2G] = "2G";
    types[plus.networkinfo.CONNECTION_CELL3G] = "3G";
    types[plus.networkinfo.CONNECTION_CELL4G] = "4G";
    console.log("Network: " + types[plus.networkinfo.getCurrentType()]);
},
监听网络状态变化
uni.onNetworkStatusChange(function (res) {
    console.log(res.isConnected);//当前是否有网络连接
    console.log(res.networkType);//网络类型
    if(res.isConnected === false){
        console.log("当前无网络");
    }else{
        console.log("有网络");
    }
});

你可能感兴趣的:(uni-app 判断网络)