如何在Ionic 4的每个页面中检查互联网连接。需要的是,如果存在网络错误,我需要将应用程序重定向到错误页面。
发布于 2018-10-30 21:01:18
你可以尝试官方的离子网络插件(文档here)。
文档中的使用示例:
import { Network } from '@ionic-native/network/ngx'; //ngx
constructor(private network: Network) { }
...
// 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!');
// We just got a connection but we need to wait briefly
// before we determine the connection type. Might need to wait.
// prior to doing any api requests as well.
setTimeout(() => {
if (this.network.type === 'wifi') {
console.log('we got a wifi connection, woohoo!');
}
}, 3000);
});
// stop connect watch
connectSubscription.unsubscribe();发布于 2018-10-30 21:01:04
使用以下命令安装网络信息插件:-
cordova plugin add cordova-plugin-network-information然后,您可以在deviceready之后使用以下代码在设备离线时执行一些代码:
document.addEventListener("offline", onOffline, false);
function onOffline() {
// Handle the offline event
// use code to redirect here
}发布于 2019-10-07 20:31:10
if (this.network.type === 'none') {
this.presentAlert('network was disconnected :-(');
setTimeout(() => {
navigator['app'].exitApp();
}, 4000);
}https://stackoverflow.com/questions/53064711
复制相似问题