Keep a reference to your toast, and call dismiss()
on it before presenting it. This solution will keep you from having more than one Toast
presented at a time.
A solution I like to use myself is to handle all Toast
interaction in a service. And inject that service in to whatever component/page/service you need it in.
ToastService:
import { Injectable } from '@angular/core';
import { ToastController, Toast } from 'ionic-angular';
@Injectable()
export class ToastService{
toast: Toast = null;
constructor(private toastCtrl: ToastController){ }
presentToast(text:string):void{
let toastData = {
message: text,
duration: 3000,
position: 'top'
}
this.showToast(toastData);
}
presentClosableToast(text:string):void{
let toastData = {
message: text,
showCloseButton: true,
closeButtonText: 'X',
position: 'top'
};
this.showToast(toastData);
}
private showToast(data:any):void{
this.toast ? this.toast.dismiss() : false;
this.toast = this.toastCtrl.create(data);
this.toast.present();
}
}