Cordova插件网络信息在Ionic 3中不起作用

时间:2018-10-10 17:37:58

标签: cordova ionic-framework ionic3 cordova-plugins ionic-native

我已经尝试过以下方法。

添加了此模块。

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,但是我却空了{}

这是stackblitz

谢谢。

1 个答案:

答案 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");

          });
        }
      }