Ionic 4 网络检查 [英] Ionic 4 network check
本文介绍了Ionic 4 网络检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查 Ionic 4 中每个页面的互联网连接.需要的是,如果出现网络错误,我需要将应用程序重定向到错误页面.
How do I check of internet connectivity in each page in Ionic 4. The need is if there is an network error I need to redirect the app to an error page.
推荐答案
你可以试试官方的 Ionic Network 插件(doc 这里).
You can try the official Ionic Network plugin (doc 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();
这篇关于Ionic 4 网络检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文