Ionic 4网络检查

编程入门 行业动态 更新时间:2024-10-24 13:19:32
本文介绍了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网络检查

本文发布于:2023-11-23 10:11:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621064.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网络   Ionic

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!