我的登录菜单使用警报消息时出现以下错误:
运行时错误未捕获(在承诺中(:假 堆栈错误:未捕获(在 承诺(:假
这是代码:
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);
}
}
我认为错误与这行代码有关:
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);
}