解除旧警报并提供最新警报 - Ionic3 [英] Dismiss old alert and present latest alert - Ionic3

查看:106
本文介绍了解除旧警报并提供最新警报 - Ionic3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ionic 3的警报,我正面临警告堆叠的问题。
我正在使用网络插件检查用户是否连接到网络(WiFi / 2G / 3G等),并且想法是每次用户下线或上线时触发警报。 / p>

I am using the alerts of Ionic 3 and I am facing an issue of alerts stacking up. I am using the network plugin to check if the user is connected to a network(WiFi/2G/3G etc.) or not and the idea is to trigger an alert every time the user goes offline or comes online.

this.connectSubscription = this.network.onConnect().subscribe(() => {
  console.log('network connected!');
  let connectedToInternet = this.alertController.create({
    subTitle: 'Connected to Internet',
    buttons: ['OK']
  });
  connectedToInternet.present();
});

this.disconnectSubscription = this.network.onDisconnect().subscribe(() => {
  let noInternetAlert = this.alertController.create({
    subTitle: 'No Internet Connection. Please enable Wifi or Mobile data to continue.',
    buttons: ['OK']
  });
  noInternetAlert.present();
});

当前行为:如果用户多次断开连接并重新连接,则每次网络中的更改实例,会显示警报,并且警报会在视图上叠加,直到用户手动关闭警报为止。

Current behaviour: If the user disconnects and reconnects multiple times, for every instance of change in the network, an alert is presented and the alerts stack up on the view until the user manually dismisses the alerts.

所需行为:如果用户多次断开连接并重新连接,则对于网络中的每个更改实例,应在旧的警报自动被解除时显示警报,以便在任何给定的时间点,不向用户显示多个实例视图上的任何警报。

Required behaviour: If the user disconnects and reconnects multiple times, for every instance of change in network, an alert should be presented while the older alert automatically gets dismissed so that at any given point in time, the user is not presented with more than one instance of any alert on the view.

推荐答案

isAvailable: Boolean; //boolean variable helps to find which alert we should dissmiss   
connectedToInternet; //made global to get access 
noInternetAlert; // made global to get access


 this.connectSubscription = this.network.onConnect().subscribe(() => {
      console.log('network connected!');

      if(!this.isAvailable){ //checking if it is false then dismiss() the no internet alert
        this.noInternetAlert.dismiss();
      }
       this.isAvailable =true;

      this.connectedToInternet = this.alertController.create({
        subTitle: 'Connected to Internet',
        buttons: ['OK']
      });
      this.connectedToInternet.present();
    });

    this.disconnectSubscription = this.network.onDisconnect().subscribe(() => {

     if(this.isAvailable){// if it is true then dismiss the connected to internet alert
        this.connectedToInternet.dismiss();
      }
      this.isAvailable = false;
      this.noInternetAlert = this.alertController.create({
        subTitle: 'No Internet Connection. Please enable Wifi or Mobile data to continue.',
        buttons: ['OK']
      });
      this.noInternetAlert.present();
    });

这篇关于解除旧警报并提供最新警报 - Ionic3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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