AlertController离子2未捕获(在承诺中):插入的视图已被破坏

时间:2017-05-24 20:27:27

标签: ionic-framework ionic2

在同一页面上实例化Ionic 2 AlertController会出现此错误:未捕获(在承诺中):已插入的视图已被销毁

我想让它运行几次等于离子1警报实例,可以在同一页面上多次调用。

代码:

  export class ConsultaProdutoPage {

    public usar_leitor: boolean = false;
    public codigo: string = '';
    public icon: object = { 'icon': 'search', 'text': 'Buscar' };
    public mostrar_produto: boolean = false;
    private loading;
    private _alert: Alert;

    constructor(public navCtrl: NavController,
        public navParams: NavParams,
        private _barcodeScanner: BarcodeScanner,
        private _alertCtrl: AlertController,
        private _service: consultaProdutoService,
        private _loadingCtrl: LoadingController) {

        this.loading = this._loadingCtrl.create({
            content: 'Buscando Produtos. Aguarde...',
            dismissOnPageChange: true
        });

        this._alert = this._alertCtrl.create({
            'title': "Aviso",
            'message': 'Erro ao buscar produtos.',
            buttons: ['OK']
        });

    }

    buscaProduto(codigo) {

        this.loading.present();

        this._service.getProduto(this.codigo)
            .then(success => {
                console.log(success);
            })
            .catch(err => {

                this.loading.dismiss();
                this.codigo = '';

                this._alert.present();

            });


    }

}

1 个答案:

答案 0 :(得分:29)

此问题是因为在您的函数中重用加载对象

由于您“希望让它多次运行”,因此加载对象也会被重用。但是,此对象只能使用一次。检查here

尝试:

buscaProduto(codigo) {
         this.loading = this._loadingCtrl.create({
            content: 'Buscando Produtos. Aguarde...',
            dismissOnPageChange: true
        });

        this.loading.present();

        this._service.getProduto(this.codigo)
            .then(success => {
                console.log(success);
            })
            .catch(err => {

                this.loading.dismiss();
                this.codigo = '';

                this._alert.present();

            });


    }