我正在将@ ionic-native / network和cordova-plugin-network-information信息插件一起用于检查脱机状态。但是我在更新网络状态时遇到困难。
home.ts
import { Network } from '@ionic-native/network';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
public online:boolean=false;
constructor(public navCtrl: NavController,private network: Network) {
this.network.onDisconnect().subscribe(() => {
this.online=false;
console.log(this.online); });
this.network.onConnect().subscribe(() => {
this.online=true;
console.log(this.online);
});
}
}
home.html
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<h1>{{online}}</h1>
</ion-content>
但是,在运行该应用程序时,它仅显示false,但是console.log(this.online)
每当我切换wifi时,它就会显示true-false。关于如何更改接口值的任何建议也是如此。
如果由于某种原因未在模板中进行更改检测,请尝试使用ChangeDetectorRef:
import { ChangeDetectorRef } from '@angular/core';
constructor(private ref: ChangeDetectorRef) { }
this.network.onDisconnect().subscribe(() => {
this.online = false;
console.log(this.online);
this.ref.detectChanges(); // run
});
this.network.onConnect().subscribe(() => {
this.online=true;
console.log(this.online);
this.ref.detectChanges(); // run
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句