未捕(承诺):错误

时间:2017-05-17 18:10:45

标签: angular typescript ionic2

我在登录菜单中使用警报消息时出现以下错误:

  

运行时错误未捕获(在承诺中):false堆栈错误:未捕获(在   承诺):假的

这是代码:

  public login() {
    this.showLoading()
    this.auth.login(this.Login).subscribe(allowed => {
      if (allowed) {        
        //this.navCtrl.setRoot('Inicio');
        this.usuarioLogueado = this.auth.getUserInfo();
        if(this.usuarioLogueado.tipo == "Administrador"){
          this.navCtrl.setRoot(Administrador);
        }
        console.log("bienvenido",this.usuarioLogueado.usuario,this.usuarioLogueado.tipo);
      } else {
        this.showError("Acceso denegado");
      }
    },
      error => {
        this.showError(error);
      });
  }

  showLoading() {
    this.loading = this.loadingCtrl.create({
      content: 'Por favor espere...',
      dismissOnPageChange: true
    });
    this.loading.present().then(() => this.loading.dismiss());
  }

  showError(text) {
this.loading.dismiss().catch(() => console.log('ERROR: Control de loading fallo'));
    let alert = this.alertCtrl.create({
      title: 'Fallo',
      subTitle: text,
      buttons: ['OK']
    });
    alert.present(prompt);
  }


}

1 个答案:

答案 0 :(得分:3)

我认为错误与这行代码有关:

this.loading.present().then(() => this.loading.dismiss());

我不确定为什么要在装载变得可见时隐藏装载。使用加载器的正确方法是在发出http请求之前显示它,并在请求结束时隐藏它。它看起来像这样:

// Assuming you already have a property to hold the instance of the loader 
public loading: any;

public login() {
    this.showLoading().then(() => { // Show the loading before making the request

        this.auth.login(this.Login).subscribe(allowed => { // Make the http request

            this.loading.dismiss().then(() => { // Hide the loading

                if (allowed) {

                    // this.navCtrl.setRoot('Inicio');
                    this.usuarioLogueado = this.auth.getUserInfo();

                    if (this.usuarioLogueado.tipo == "Administrador") {
                        this.navCtrl.setRoot(Administrador);
                    }

                    console.log("bienvenido", this.usuarioLogueado.usuario, this.usuarioLogueado.tipo);

                } else {
                    this.showError("Acceso denegado");
                }
            });
        }, error => {
            this.loading.dismiss().then(() => { // Hide the loading
                this.showError(error);
            });
        });
    });

}

showLoading(): Promise<any> { // <- Return the promise
    this.loading = this.loadingCtrl.create({
        content: 'Por favor espere...',
        dismissOnPageChange: true
    });
    return this.loading.present(); // <- Added the return keyword here
}

showError(text) {
    let alert = this.alertCtrl.create({
        title: 'Fallo',
        subTitle: text,
        buttons: ['OK']
    });
    alert.present(prompt);
}