我已经尝试过以下方法。
添加了此模块。
ionic cordova plugin add cordova-plugin-network-information
npm install --save @ionic-native/network
还在我的Network
中添加了app.module.ts
import { Network } from '@ionic-native/network';
@NgModule({
...
providers: [Network]
})
export class AppModule {}
已在app.component.ts
内部使用过:
import { Network } from '@ionic-native/network';
constructor(platform: Platform, private network: Network) {
platform.ready().then(() => {
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
console.log('network was disconnected !!');
});
let connectSubscription = this.network.onConnect().subscribe(() => {
console.log('network connected!');
setTimeout(() => {
if (this.network.type === 'wifi') {
console.log('we got a wifi connection, woohoo!');
}
}, 3000);
});
});
}
我在控制台中什么也没得到。
问题是当我的应用程序初始化控制台没有被触发时,我也试图控制台
network
,但是我却空了{}
。
谢谢。
答案 0 :(得分:0)
这仅适用于realDevices或模拟器
constructor(private network: Network) {
this.network_check();
}
network_check() {
if (this.network.type === 'WIFI' || 'CELL_3G' || 'CELL_4G') {
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
alert("Disconnected");
});
}
}