Ionic:如何不叠加多个Toast通知? [英] Ionic: How to not stack multiple toast notifications?
问题描述
我得到了以下Ionic代码片段,用于在工业应用中显示警报/错误:
I got the following Ionic code fragment for displaying alarms / errors in an industrial App:
showError(message: string) {
let toast = this.toastController.create({
message: message,
position: 'top',
duration: 5000,
cssClass: 'danger',
showCloseButton: true
});
toast.present();
}
应用程序每次检测到连接问题时都会触发错误消息,也大致在5秒计时器上。
The App triggers the error message every time it detects a connection issues, which will be also roughly on a 5 second timer.
如果此代码的时间是,则多次调用此方法将导致两个或更多错误消息显示在彼此之上改变。我可以以某种方式检测到已经显示吐司吗?此外,5000毫秒定时器不是必需的,我可以在重新建立连接时再次删除错误消息。
Multiple calls to this method will lead to 2 or more error messages shown on top of each other if the timing of this code is changed. Can I somehow detect that a toast is already being displayed? Also then, the 5000 msec timer would not be necessary and I can just remove the error message again when the connection is re-established.
谢谢和BR Florian
Thanks and BR Florian
推荐答案
您可以将Toast对象存储在函数外部的变量中,并在显示下一个toast之前调用dismiss()方法:
You could store your Toast object in a variable outside your function, and call the dismiss() method before showing the next toast :
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();
}
这篇关于Ionic:如何不叠加多个Toast通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!