本文介绍了Ionic 4网络检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Ionic 4的每个页面中检查Internet连接.需要是是否存在网络错误,我需要将应用程序重定向到错误页面.
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插件(文档此处).
You can try the official Ionic Network plugin (doc here).
doc中的用法示例:
Usage example from doc :
import { Network } from '@ionic-native/network/ngx'; //ngx constructor(private network: Network) { } ... // watch network for a disconnect let disconnectSubscription = thiswork.onDisconnect().subscribe(() => { console.log('network was disconnected :-('); }); // stop disconnect watch disconnectSubscription.unsubscribe(); // watch network for a connection let connectSubscription = thiswork.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 (thiswork.type === 'wifi') { console.log('we got a wifi connection, woohoo!'); } }, 3000); }); // stop connect watch connectSubscription.unsubscribe();更多推荐
Ionic 4网络检查
发布评论