cordova 离子:如何不堆叠多个吐司通知?

8ehkhllq  于 2022-11-15  发布在  其他
关注(0)|答案(8)|浏览(134)

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

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

应用程序在每次检测到连接问题时都会触发错误消息,这也将大约在5秒计时器上。
如果更改此代码的计时,多次调用此方法将导致2条或更多错误消息重叠显示。我是否可以检测到吐司已经显示?此外,5000毫秒计时器将不再必要,我可以在重新建立连接时再次删 debugging 误消息。
谢谢,BR弗洛里安

cygmwpex

cygmwpex1#

您可以将吐司对象存储在函数外部的变量中,并在显示下一个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();
}
uemypmqf

uemypmqf2#

这里我的解决方案:-)

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

}
0md85ypi

0md85ypi3#

您可以检查是否已存在吐司,并仅在没有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');
    });

}
gstyhher

gstyhher4#

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

t98cgbkg5#

在Ionic 4吐司UI组件中

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

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

vu8f3i0k

vu8f3i0k6#

我用这种方法解决了

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

  }
mfpqipee

mfpqipee7#

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

    }
lmvvr0a8

lmvvr0a88#

依次显示具有持续时间的Toast队列。

离子6

toastCtrl(color,msg,duration?) {
    const toast = {
      message: msg,
      color: color,
      duration: duration || 3000
    };
    this.toasts.push(toast)
    const timeout = (this.toasts.length - 1) * toast.duration
    this.show(timeout);
  }
  show(timeout) {
    setTimeout(async () => {
      const toast = await this.toastController.create(this.toasts[0]);
      await toast.present();
      this.toasts.splice(0, 1);
    }, timeout > 0 ? timeout + 800 : 0);

  }

相关问题