Ionic 4网络检查 [英] Ionic 4 network check

查看:123
本文介绍了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 = 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆