我正在尝试在我的应用中查看我的设备上的互联网连接。
我一直在看ionic documentation
我编写了以下代码并且可以正常工作
// watch network for a disconnect
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
console.log('network was disconnected :-(');
});
// stop disconnect watch
disconnectSubscription.unsubscribe();
// watch network for a connection
let connectSubscription = this.network.onConnect().subscribe(() => {
console.log('network connected!');
});
// stop connect watch
connectSubscription.unsubscribe();
然后我尝试在我自己的功能中使用它
//import
import { Network } from '@ionic-native/network';
//declare
public check : boolean = true;
public check2 : boolean = false;
//constructor
constructor(public http: Http,private network: Network) {
}
//functions
isOffline(){
let disconnectSub = this.network.onDisconnect().subscribe(() => {
this.check = false
});
disconnectSubscription.unsubscribe();
return check;
}
isOnline(){
let connectSub = this.network.onConnect().subscribe(()=> {
this.check2 = true;
});
connectSubscription.unsubscribe();
return check;
}
然后我从一个按钮调用我使用console.log()
,但有关连接的数据不正确