ionic2 toast 提示框

import { Injectable } from '@angular/core';

import { Toast, ToastController, ToastOptions } from 'ionic-angular';

@Injectable()

export class ToastControllerService {

    private toast: Toast = null;

    constructor(private tc: ToastController) {}

    /**

     * 打开toast

     * @param message 提示信息

     * @param position 位置(top,middle,bottom) 默认 bottom下部

     * @param duration 显示时长 默认3000ms

     * @param showCloseButton 是否显示关闭按钮 默认不显示

     */

    public open(message: string, position: string = "bottom", duration: number = 3000, showCloseButton: boolean=false): void {

        let option: ToastOptions = { message: message, position: position, duration: duration, showCloseButton: showCloseButton, closeButtonText: '关闭', dismissOnPageChange: true };

        if (this.toast) {

            try {

                this.toast.dismiss().then(() => {

                    this.toast = this.tc.create(option)

                    this.toast.present().catch((e) => {

                        console.log(e);

                    });

                }).catch((e) => {

                    console.log(e);

                });

            } catch (e) {

                console.log(e);

            }

        } else {

            this.toast = this.tc.create(option)

            this.toast.present().catch((e) => {

                console.log(e);

            });

        }

    }

    /**

     * 关闭组件

     */

    public closed(): void {

        if (this.toast) {

            try {

                this.toast.dismiss().then(() => {

                    this.toast = null;

                }).catch((e) => {

                    console.log(e);

                });

            } catch (e) {

                console.log(e);

            }

        }

    }

}

猜你喜欢

转载自blog.csdn.net/xin_bao_long/article/details/81943641