离子3警报;调用dismiss()后调用present()不工作

问题描述 投票:0回答:2

我的Ionic应用程序中有网络检测功能。目标是在没有网络时阻止用户进行交互。

代码如下页面组件:

ionViewDidEnter() {
this.alert = this.alertCtrl.create({
      title: 'No Network!',
      subTitle: 'Please Connect to Internet to Continue',
      enableBackdropDismiss: false
    });

    if(this.networkCheck.checkNetworkType() === 'none' || this.networkCheck.checkNetworkType() === 'unknown'){
      this.alert.present();
    }
this.networkSubscription = this.networkCheck.onStatusChange().subscribe(
      data => {
          console.debug(data);

          if(data.type === 'offline'){
              this.alert.present();
              this.toast.show(data.type);
          }
          else if(data.type === 'online'){
              this.alert.dismiss();
              this.toast.show(data.type);
          }
      })
}

ionViewWillLeave() {
    this.networkSubscription.unsubscribe();
  }

并在提供者:

onStatusChange(){
    return this.network.onchange()
  }

问题是当Toast显示所有状态更改时,警报弹出窗口仅在网络断开时第一次显示。在它第一次被解雇后,它没有再次出现。

你能帮我解决一下这个问题吗?我猜视某些事情,因为这就是为什么会发生这种情况的原因,但是我无法控制住这一点。

如果有更好的方法来处理这种行为,请告诉我。

angular ionic3
2个回答
0
投票

你可以尝试这个解决方案。

alert:any;
showAlert(){
  this.alert = this.alertCtrl.create({
    title: 'No Network!',
    subTitle: 'Please Connect to Internet to Continue',
    enableBackdropDismiss: false
  });
  this.alert.present();
}
ionViewDidEnter() {


  if (this.networkCheck.checkNetworkType() === 'none' || this.networkCheck.checkNetworkType() === 'unknown') {
    this.showAlert();
  }
  this.networkSubscription = this.networkCheck.onStatusChange().subscribe(
    data => {
      console.debug(data);

      if (data.type === 'offline') {
        this.showAlert();
        this.toast.show(data.type);
      }
      else if (data.type === 'online') {
        this.alert && this.alert.dismiss();
        this.toast.show(data.type);
      }
    })
}

ionViewWillLeave() {
  this.networkSubscription.unsubscribe();
}

0
投票

您可以在app.component.ts文件中编写代码,以便它适用于所有页面。

import { Component, ViewChild } from '@angular/core';
import { Platform, Nav, Alert, AlertController } from 'ionic-angular';
import { Network } from '@ionic-native/network';
import { Subscription } from 'rxjs';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;

  networkWarningAlert: Alert = null;
  disconnectSubscription: Subscription = null;
  connectSubscription: Subscription = null;

  constructor(public platform: Platform,
    public alert: AlertController,
    private network: Network) {
    platform.ready().then(() => {
         this.SubscribeNetworkAction();
    });
  }

  isConnected(): boolean {
    let conntype = this.network.type;
    return conntype && conntype !== 'unknown' && conntype !== 'none';
  }

  SubscribeNetworkAction() {

    if (!this.platform.is("cordova"))
      return;

    if (!this.isConnected() && !this.networkWarningAlert) {
      this.networkWarningAlert = this.alert.create({
        title: 'No network',
        message: 'Check your internet connection',
        enableBackdropDismiss: false,
        buttons: [{
          text: "Exit",
          handler: () => { this.platform.exitApp(); }
        }]
      })
      this.networkWarningAlert.present();
    }

    this.disconnectSubscription = this.network.onDisconnect().subscribe(() => {
      if (!this.networkWarningAlert) {
        this.networkWarningAlert = this.alert.create({
          title: 'No network',
          message: 'Check your internet connection',
          enableBackdropDismiss: false,
          buttons: [{
            text: "Exit",
            handler: () => { this.platform.exitApp(); }
          }]
        })
        this.networkWarningAlert.present();
      }
    });

    this.connectSubscription = this.network.onConnect().subscribe(() => {
      if (this.networkWarningAlert) {
        this.networkWarningAlert.dismiss();
        this.networkWarningAlert = null;
      }
      // this code is used for refresh current page.
      var currentStack = this.nav.getViews();
      this.nav.setRoot(currentStack[0].component);
      for (var i = 1; i < currentStack.length; i++) {
        this.nav.push(currentStack[i].component, currentStack[i].getNavParams());
      }
    });
  }

  UnSubscribeNetworkAction() {
    if (!this.platform.is("cordova"))
      return;

    if (this.disconnectSubscription) {
      this.disconnectSubscription.unsubscribe();
      this.disconnectSubscription = null;
    }
    if (this.connectSubscription) {
      this.connectSubscription.unsubscribe();
      this.connectSubscription = null;
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.