Prevent duplicate Toast messages in Ionic

后端 未结 2 484
北荒
北荒 2021-01-18 09:42

I have implemented toast using ToastController in my ionic2 project . Currently i am facing an issue with the duplicate toast

相关标签:
2条回答
  • 2021-01-18 10:01

    You can use a property on that page to know if a toast is being shown or not before showing a new one.

    Ionic 2/3

    import { ToastController, Toast } from 'ionic-angular';
    
    // ...
    
    private isToastVisible: boolean;
    
    constructor(private toastCtrl: ToastController) { }
    
    presentToast() {
      if(this.isToastVisible) {
        return;
      }
    
      this.isToastVisible = true;
    
      const toast: Toast = this.toastCtrl.create({
        message: 'User was added successfully',
        duration: 3000,
        position: 'top'
      });
    
      toast.onDidDismiss(() => {
        this.isToastVisible = false;
      });
    
      toast.present();
    }
    

    Ionic 4/5

    import { ToastController } from '@ionic/angular';
    
    // ...
    
    private isToastVisible: boolean;
    
    constructor(private toastCtrl: ToastController) { }
    
    presentToast() {
      if(this.isToastVisible) {
        return;
      }
    
      this.isToastVisible = true;
    
      this.toastCtrl.create({
        message: 'User was added successfully',
        duration: 3000,
        position: 'top'
      }).then((toast: HTMLIonToastElement) => {
    
        toast.onDidDismiss().then(() => {
          this.isToastVisible = false;
        });
    
        toast.present();
      })      
    }
    
    0 讨论(0)
  • 2021-01-18 10:06

    I set preventDuplicates to 1 in my toastr.js file, please see below;

    function p() {
        return {
            tapToDismiss: !0,
            toastClass: "toast",
            containerId: "toast-container",
            debug: !1,
            showMethod: "fadeIn",
            showDuration: 300,
            showEasing: "swing",
            onShown: void 0,
            hideMethod: "fadeOut",
            hideDuration: 1e3,
            hideEasing: "swing",
            onHidden: void 0,
            closeMethod: !1,
            closeDuration: !1,
            closeEasing: !1,
            closeOnHover: !0,
            extendedTimeOut: 1e3,
            iconClasses: {
                error: "toast-error",
                info: "toast-info",
                success: "toast-success",
                warning: "toast-warning"
            },
            iconClass: "toast-info",
            positionClass: "toast-top-right",
            timeOut: 2e3,
            titleClass: "toast-title",
            messageClass: "toast-message",
            escapeHtml: !1,
            target: "body",
            // closeButton: true,
            closeHtml: '<button type="button">&times;</button>',
            closeClass: "toast-close-button",
            newestOnTop: !0,
            preventDuplicates: 1,
            progressBar: 1,
            progressClass: "toast-progress",
            rtl: !1
        }
    }
    
    0 讨论(0)
提交回复
热议问题