Prevent duplicate Toast messages in ionic2

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 18:29:31

问题


I have implemented toast using ToastController in my ionic2 project . Currently i am facing an issue with the duplicate toast messages . Is there any way to prevent the duplication / overlapping of toast message in ionic2 / angular2

(NB : Duplication means when I click on a button I am displaying a toast , if I click on the same button multiple times the toast messages overlaps ) ?

code

export class <className>{
   constructor(private toast:ToastController){
   }
    showToast(message) {
    let toast = this.toastCtrl.create({
        message: message,
        duration: 2000,
        position: 'bottom'
    })
    toast.present();
   }
}

I am calling this method on an button click .

Edited

  1. with duplicates toast (taken example using toastr , same sitaution is for me)

  2. when i enable "prevent notification" , the duplicate toast are not there within the timeout duration .

Any help is much appreciated.


回答1:


You can use a property from that page to keep the instance of the toast. Then, after showing a toast, verify if another is being shown.

import { ToastController, Toast } from 'ionic-angular';

// ...

private toastInstance: Toast;

constructor(private toastCtrl: ToastController) { }

presentToast() {

  if(this.toastInstance) {
    return;
  }

  this.toastInstance = this.toastCtrl.create({
    message: 'User was added successfully',
    duration: 3000,
    position: 'top'
  });

  this.toastInstance.onDidDismiss(() => {
    this.toastInstance = null;
  });

  this.toastInstance.present();
}



回答2:


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
    }
}


来源:https://stackoverflow.com/questions/45074161/prevent-duplicate-toast-messages-in-ionic2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!