【问题标题】:Prevent multiple Ionic Alerts from stacking up防止多个离子警报堆积
【发布时间】:2020-10-14 00:47:33
【问题描述】:

我如何检测 ionic 2 alert ui component 实例是否已经打开以便不显示另一个警报?

【问题讨论】:

    标签: ionic-framework ionic2


    【解决方案1】:

    我最终为 Ionic 的 Alert 控制器编写了一个包装提供程序,如下所示:

    import { Injectable } from '@angular/core';
    import { AlertController } from 'ionic-angular';
    
    @Injectable()
    export class Alert {
      public alertPresented: any;
      constructor(public alertCtrl: AlertController) {
        this.alertPresented = false
      }
    
      present(title, subTitle) {
        let vm = this
        if(!vm.alertPresented) {
          vm.alertPresented = true
          vm.alertCtrl.create({
            title: title,
            subTitle: subTitle,
            buttons: [{
              text: 'OK',
              handler: () => {
                vm.alertPresented = false
              }
            }],
          }).present();
        }
      }
    }
    

    alertPresented 标志可防止出现多个实例

    【讨论】:

    • 这只会阻止创建新警报。这不会自动关闭旧警报并显示最新警报,对吧?
    • 知道如何自动关闭旧警报并显示新警报吗?
    • 我为此创建了一个问题:stackoverflow.com/questions/45919152/…
    【解决方案2】:

    我有另一个想法,您可以为变量分配消息并检查新消息是否等于它。如果相等,返回。 这是我的代码,希望你喜欢它。

    import { Injectable } from '@angular/core';
    import { AlertController } from 'ionic-angular';
    
    @Injectable()
    export class AlertProvider {
    
      public showingMessage = ""
    
      constructor(
        private alertController: AlertController
      ) {}
    
      showAlert(message) {
        // Check this message is showing or not
        if (message === this.showingMessage) {
          return
        }
    
        this.showingMessage = message
        this.alertController.create({
          title: "APP_NAME",
          message: message,
          buttons: [{
            text: "OK",
            handler: () => {
              this.showingMessage = ""
            }
          }]
        }).present()
      }
    }
    

    【讨论】:

      【解决方案3】:

      您可以创建一个AlertService 来处理更多选项,而无需为按钮注入事件

      import { Injectable } from '@angular/core';
      import { AlertController, Alert } from 'ionic-angular';
      
      /**
       * A simple alert class to show only one alert at the same time
       */
      @Injectable()
      export class AlertService {
        currentAlert: Alert
        constructor(private alertCtrl: AlertController) {
        }
      
        show(title, message, buttons: any = [], inputs: any = [], cssClass = '') {
          if (!buttons.length) {
            buttons.push('Ok')
          }
          let alertOptions: any = {
            title: title,
            subTitle: message,
            buttons: buttons,
            cssClass: buttons.length === 2 ? 'confirmAlert' : cssClass
          }
          if (inputs.length) {
            alertOptions.inputs = inputs
          }
          if (!this.currentAlert) {
            this.currentAlert = this.alertCtrl.create(alertOptions)
            this.currentAlert.present()
            this.currentAlert.onDidDismiss(() => {
              this.currentAlert = null
            })
          }
          return this.currentAlert
        }
      }
      

      问候,尼科尔斯

      【讨论】:

        【解决方案4】:

        我的解决方案有效,我必须在取消事件后放置一个布尔值并将其设置为 true,并在出现警报时将其设置为 false

        if (this.network_alert) {
              let alert = await this.alertController.create({
                header: "No Network",
                message:
                  "Please check your internet connection",
                buttons: [{
                  text: "Dismiss",
                  role: 'cancel',
                  handler: () => {
                    console.log('Cancel clicked');
                    this.network_alert = true
                  }
                }],
        
              });
              await alert.present();
              this.network_alert = false
            }
          }
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-22
        • 1970-01-01
        • 2018-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多