2

I use toast but if I have 2+ toasts open at the same time the toasts wont close.

let toast = this.toastCtrl.create({
      message: 'İnternet bağlantınızda veya sunucuda sorun olabilir.',
      duration: 3000,
      position: 'bottom'
});
toast.present();
Jakegarbo
  • 1,201
  • 10
  • 20
Ahmet emre CETIN
  • 321
  • 1
  • 4
  • 22
  • 1
    Show your whole .ts page please. I think you created infinitive toast. I tested in my project with 2+ tab at a same time and they actually dismissed. – Duannx Aug 09 '17 at 01:47

1 Answers1

2

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();
    }
}
robbannn
  • 5,001
  • 1
  • 32
  • 47
  • Services shouldn't be used for UI interaction. See [here](https://stackoverflow.com/a/43030491/2493235) for an alternative solution. –  Jul 02 '19 at 00:42