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

Fel*_*ire 8 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();

            });


    }

}
Run Code Online (Sandbox Code Playgroud)

Sur*_*Rao 30

这个问题是因为在函数中重用了加载对象.

既然你"想让它运行几次",那么加载对象也会被重用.但是,此对象只能使用一次.请点击这里.

尝试:

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();

            });


    }
Run Code Online (Sandbox Code Playgroud)

  • 这也适用于`LoadingController` (2认同)