我想始终显示internet
connection
是否存在?没有
subscribing
network
changes
。
我的问题:在启动应用程序时,我想检查互联网连接。如果不存在,我想提交notification
这就是我所做的:
如果没有互联网连接,那么我想推送登录页面
以下代码在perfect
network
上运行changes
,但我无法检测是否已连接以推送loginPage?
displayNetworkUpdate(connectionState: string){
let networkType = this.network.type;
this.toastCtrl.create({
message: `You are now ${connectionState} via ${networkType}`,
duration: 3000
}).present();
}
ionViewDidEnter() {
this.network.onConnect().subscribe(data => {
console.log(data)
this.displayNetworkUpdate(data.type);
}, error => console.error(error));
this.network.onDisconnect().subscribe(data => {
console.log(data)
this.displayNetworkUpdate(data.type);
}, error => console.error(error));
}
以上代码在perfect
network
上运行changes
,但我无法检测是否已连接推送loginPage的状态
答案 0 :(得分:0)
在根组件中,请关注offline
事件
window.addEventListener('offline', ()=>{
// your logic
});
答案 1 :(得分:0)
请参阅此链接,
https://ionicframework.com/docs/native/network/
import { Network } from '@ionic-native/network';
constructor(private network: Network) { }
if (this.network.type === 'none') {
console.log('No connection');
}
答案 2 :(得分:0)
将以下方法放在要启动的第一页的构造函数中。在断开连接时关闭活动门户,如模态,吐司和覆盖,导航到其他页面(网络页面 - 没有互联网消息或登录页面的页面)将不会关闭活动门户。
import {IonicApp } from 'ionic-angular';
import { Network } from '@ionic-native/network';
导入Ionic App和网络以运行以下代码
this.network.onDisconnect().subscribe(() => {
let activePortal = this.ionicApp._loadingPortal.getActive() ||
this.ionicApp._modalPortal.getActive() ||
this.ionicApp._toastPortal.getActive() ||
this.ionicApp._overlayPortal.getActive();
//activePortal is the active overlay like a modal,toast,etc
if (activePortal) {
activePortal.dismiss();
}
this.navCtrl.setRoot(NetworkPage);
});