app 判断网络状态

↘锁芯ラ 提交于 2020-03-29 07:14:06
cordova plugin add cordova-plugin-network-information

  

 mounted(){
  this.checkConnection()
  document.addEventListener("offline", this.onOffline(), false);
 }

  

    checkConnection (){
        let networkState = navigator.connection.type;
        let states = {};
        states[Connection.UNKNOWN]  = 'Unknown connection';
        states[Connection.ETHERNET] = 'Ethernet connection';
        states[Connection.WIFI]     = 'WiFi connection';
        states[Connection.CELL_2G]  = 'Cell 2G connection';
        states[Connection.CELL_3G]  = 'Cell 3G connection';
        states[Connection.CELL_4G]  = 'Cell 4G connection';
        states[Connection.CELL]     = 'Cell generic connection';
        states[Connection.NONE]     = 'No network connection';
        if(networkState==Connection.NONE){
          alert('主人,又没网络啦')
        }else {
          alert('网络类型' + states[networkState]);
        }
      },
      onOffline(){
        let networkState = navigator.connection.type;
        if (networkState === Connection.NONE) {
          alert('主人,又没网络啦')
        }
      }

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!