Ionic:如何不叠加多个Toast通知?

时间:2017-02-09 09:47:29

标签: cordova angular ionic-framework

我得到了以下用于在工业应用程序中显示警报/错误的离子代码片段:

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

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

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

谢谢,BR Florian

7 个答案:

答案 0 :(得分:5)

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

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();
}

答案 1 :(得分: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();
  }

}

答案 2 :(得分:0)

您可以检查是否已有烤面包,并且仅在没有烤面包的情况下创建新的烤面包。

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');
    });

}

答案 3 :(得分:0)

在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();
  });
}

enter image description here

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

答案 4 :(得分:0)

我用这种方式解决了

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();
    }

  }

答案 5 :(得分:0)

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

    }

答案 6 :(得分:0)

我在使用aug 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));
  }
}