【问题标题】:Ionic: How to not stack multiple toast notifications?Ionic:如何不堆叠多个 toast 通知?
【发布时间】:2017-06-27 06:10:15
【问题描述】:

我得到以下 Ionic 代码片段,用于在工业应用中显示警报/错误:

showError(message: string) {
  let toast = this.toastController.create({
      message: message,
      position: 'top',
      duration: 5000,
      cssClass: 'danger',
      showCloseButton: true
  });
  toast.present();
}

应用程序每次检测到连接问题时都会触发错误消息,这也将大致在 5 秒计时器上。

如果更改此代码的时间,多次调用此方法将导致 2 条或更多条错误消息重叠显示。我能以某种方式检测到吐司已经在显示吗?此外,不需要 5000 毫秒计时器,我可以在重新建立连接时再次删除错误消息。

谢谢和BR弗洛里安

【问题讨论】:

    标签: cordova angular ionic-framework


    【解决方案1】:

    您可以将 Toast 对象存储在函数外部的变量中,并在显示下一个 toast 之前调用 dismiss() 方法:

    离子 4

    import { ToastController } from '@ionic/angular';
    
    
    toast: HTMLIonToastElement;    
    
    showError(message: string) {
        try {
            this.toast.dismiss();
        } catch(e) {}
    
        this.toast = this.toastController.create({
            message: message,
            position: 'top',
            duration: 5000,
            color: 'danger',
            showCloseButton: true
        });
        toast.present();
    }
    

    离子 3

    import { ToastController, Toast } from 'ionic-angular';
    
    
    toast: Toast;    
    
    showError(message: string) {
        try {
            this.toast.dismiss();
        } catch(e) {}
    
        this.toast = this.toastController.create({
            message: message,
            position: 'top',
            duration: 5000,
            cssClass: 'danger',
            showCloseButton: true
        });
        toast.present();
    }
    

    【讨论】:

    • 嗨,需要澄清一下。上面的代码不能防止重复吐司,对吧?它解除了 toast 并再次创建一个新的。我说的对吗?
    • 我不是 100% 确定这一点,但我认为当你调用 dismiss() 时,toast 也会从 DOM 中删除
    • 如果 Toast 是自解散的,那么调用dismiss() 会报错。
    • 这就是为什么您可以将其包装在try / catch 中以在调用dismiss() 时捕获错误
    【解决方案2】:

    这是我的解决方案:-)

    // ToastService.ts
    import { Injectable } from '@angular/core';
    import { ToastController, Toast } from 'ionic-angular';
    
    @Injectable()
    export class ToastService {
    
      private toasts: Toast[] = [];
    
      constructor(private toastCtrl: ToastController) {}
    
      push(msg) {
        let toast = this.toastCtrl.create({
          message: msg,
          duration: 1500,
          position: 'bottom'
        });
    
        toast.onDidDismiss(() => {
          this.toasts.shift()
          if (this.toasts.length > 0) {
            this.show()
          }
        })
    
        this.toasts.push(toast)
    
        if (this.toasts.length === 1) {
          this.show()
        }
      }
    
      show() {
        this.toasts[0].present();
      }
    
    }
    

    【讨论】:

      【解决方案3】:

      您可以检查是否已经存在 toast,只有在没有 toast 存在时才创建新的。

      import { ToastController, Toast } from 'ionic-angular';
      
      
      toast: Toast;    
      isToastPresent:boolean=false;
      
      show(){
      
      this.isToastPresent?'':this.showError('No network');
      
      }
      
      showError(message: string) {
      
          this.toast = this.toastController.create({
              message: message,
              duration: 3000,
              cssClass: 'danger',
              showCloseButton: true
          });
          toast.present();
           this.isToastPresent=true;
      
          this.toast.onDidDismiss(() => {
            this.isToastPresent=false;
            console.log('Dismissed toast');
          });
      
      }
      

      【讨论】:

        【解决方案4】:

        我在 8 月 20 日之前使用所有答案时遇到了麻烦。吐司仍然会出现多次。修复它就像检查和设置布尔值以继续或不继续一样简单。直接设置为true,就不会运行多次了。

        isToastPresent = false;
        
        async presentToast(message: string, color?: Colors, header?: string): Promise<void> {
          if (!this.isToastPresent) {
            this.isToastPresent = true;
        
            const toast = await this.toastController.create({
              message: message,
              header: header || undefined,
              duration: 3500,
              position: 'top',
              color: color || Colors.dark,
            });
        
            toast.present();
        
            toast.onDidDismiss().then(() => (this.isToastPresent = false));
          }
        }
        

        【讨论】:

          【解决方案5】:

          在 Ionic 4 Toast UI 组件中

          使用以下代码仅显示一次 Ionic 4 Toast

          // Call this method  
          showOnceToast(){
            this.toastController.dismiss().then((obj)=>{
            }).catch(()=>{
            }).finally(()=>{
              this.manageToast();
            });
          }
          
          manageToast() {
            this.toastInstance = this.toastController.create({
              message: 'Your settings have been saved.',
              duration: 2000,
              animated: true,
              showCloseButton: true,
              closeButtonText: "OK",
              cssClass: "my-custom-class",
              position: "middle"
            }).then((obj) => {
              obj.present();
            });
          }
          

          来源链接:http://www.freakyjolly.com/ionic-4-adding-toasts-in-ionic-4-application-using-ui-components-with-plugins/

          【讨论】:

            【解决方案6】:

            我用这种方法解决了

            private mensajeErrorEnvioTramiteActivo = false;
              mensajeErrorEnvioTramite() {
                if (!this.mensajeErrorEnvioTramiteActivo) {
                  this.mensajeErrorEnvioTramiteActivo = true;
                  let toast = this.toastCtrl.create({
                    message: "No se pudo enviar los tramites formalizados, por favor reenvielos",
                    position: 'top',
                    showCloseButton: true,
                    closeButtonText: 'REENVIAR',
                  });
            
                  toast.onDidDismiss(() => {
                    this.reenvioKits.reenviarTramites();
                    this.mensajeErrorEnvioTramiteActivo = false;
                  });
                  toast.present();
                }
            
              }
            

            【讨论】:

              【解决方案7】:
              import { ToastController, Toast } from 'ionic-angular';
              
              .........
              
              private toast: Toast;
              
              .........
              
                  export Class ToastService{
              
              .........
              
                       showToastMessage(messageData) {
              
                          if (this.toast) this.toast.dismiss();
              
                          this.toast = this.toastCtrl.create({
                            message: messageData,
                            cssClass: "error-toast-cls",
                            dismissOnPageChange: true,
                            showCloseButton: true,
                            closeButtonText: "Enable"
                          });
                          this.toast.onDidDismiss((data, role) => {
                            if (role == 'close') {
                              this.diagnostic.switchToWirelessSettings()
                            }
                          })
                          await this.toast.present()  
                   }
              
                  }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2018-03-05
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-10-07
                • 1970-01-01
                • 2010-11-05
                相关资源
                最近更新 更多