【发布时间】:2020-06-17 04:48:36
【问题描述】:
当用户尝试登录时,我正在显示一个 LoadingController。同时,正在调用 API。
当我从 subscribe 收到 SUCCESS 响应时,我可以关闭 LoadingController,但是当我收到 ERROR 响应时,我无法关闭。请帮忙!
我是一名专业的 Python 开发人员,也是 Ionic 的新手,一天前才开始。所以,请提供帮助。
import { Component, OnInit } from '@angular/core';
import { ToastController, LoadingController } from '@ionic/angular';
import { CallapiService } from '../callapi.service';
@Component({
selector: 'app-login',
templateUrl: './login.page.html',
styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
userEmail = '';
userPassword = '';
loginUrl = 'login/';
loginMethod = 'POST';
postBody = {};
constructor(
public toastController: ToastController,
public loadingController: LoadingController,
private callApiService: CallapiService,
) { }
ngOnInit() {
}
async presentToast(displayMessage) {
const toast = await this.toastController.create({
message: displayMessage,
duration: 2000,
position: 'middle',
});
return await toast.present();
}
async presentLoading(loadingMessage) {
const loading = await this.loadingController.create({
message: loadingMessage,
});
return await loading.present();
}
loginUser() {
if (this.userEmail === '' || this.userPassword === '') {
this.presentToast('Email and password are required.');
}
else {
this.presentLoading('Processing...');
this.postBody = {
email: this.userEmail,
password: this.userPassword,
};
this.callApiService.callApi(this.loginUrl, this.postBody, this.loginMethod).subscribe(
(success) => {
console.log(success);
this.loadingController.dismiss();
},
(error) => {
console.log(error);
this.loadingController.dismiss();
}
);
this.loadingController.dismiss();
}
}
}
【问题讨论】:
-
尝试调试并确保您的控件在出错时进入错误块。您的服务器可能会发送带有自定义错误消息的 200。或者你可以dismiss你在
complete回调中的加载,只需在错误块后附加() => { ... }。 -
我认为您不需要最后一个
this.loadingController.dismiss()。加载控制器可能在您的 API 还没有返回之前就被关闭了。
标签: javascript angular typescript ionic-framework ionic4