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('主人,又没网络啦') } }
来源:https://www.cnblogs.com/jsusu/p/7905095.html