Ionic2和cordova-plugin-network-information

时间:2017-03-27 06:13:24

标签: cordova ionic2 cordova-plugins

我正在使用Ionic2 rc4,并想检查网络连接。在this documentationthis documentation之后,我安装了插件:

$ ionic plugin add cordova-plugin-network-information
$ npm install --save @ionic-native/network

platform.ready().then(() => {
    ...checkNetwork()();
});

public checkNetwork(): void {

    document.addEventListener("offline", this.offline, false);
    document.addEventListener("online", this.online, false);

    Network.onConnect().subscribe(()=> {
        console.log('Network.type = ', Network.connection, Network.name);
        this.online();
    });

}

offline() {
    alert('offline');
}

online() {
    alert('online');
}

offline()online()函数永远不会被调用。

更新

Networkproviders中的app.modules.ts中定义。

将代码更新为:

import { Network } from 'ionic-native';

constructor(public network: Network) 

    this.network.onConnect().subscribe(()=> {
        console.log('this.network.type = ', this.network);
        this.online();
    });

enter image description here

0 个答案:

没有答案