对登录菜单使用警报消息时出现以下错误:
未捕获的运行时错误(in promise):错误堆栈错误:未捕获(in
承诺):虚假
这是代码:
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);
}
关于angular - 未捕获( promise ):false,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44032065/