首页 > 解决方案 > 向我们的用户显示可观察到的错误消息

问题描述

我在 Login.ts 中有以下方法

login(form) {
  this.authService.login(form.value).subscribe(
  result => { // your result from observer.next() in auth.service
    // success
    this.router.navigateByUrl('tabs');
  },
  error => { // your error from observer.error() in auth.servi
    // I wish to display my error message here
);
} 

这会在 authService.ts 中调用以下方法

 login(postData: User): Observable<any> {
this.presentLoading('Attempting to Login');
return new Observable(observer => {
  this.http.post(this.AUTH_SERVER_ADDRESS + `/login`, postData)
    .pipe(
      catchError(error => {
        this.loadingController.dismiss();
        return throwError(error); // if you catch any errors, pass them (rethrow) to the error block
      })
     )
     .subscribe(
       result => {
        this.loadingController.dismiss();
        this.UserIsAuthenticated = true;
        this.setUserID(postData);
        observer.next(result); // if you receive a result, pass to register function in register.page.ts
        observer.complete(); // close the observable
       },
       error => {
        this.loadingController.dismiss();
        alert(error);
        observer.error(error); // if you receive an error, pass error to register function
        observer.complete(); // close the observable
        }
      );
    });

}

如果登录方法中发生错误,我想通过警报通知用户并详细说明错误。但是我不确定如何在我的评论中指出如何在 Login.ts 文件中向用户显示此消息。我努力了

alert(error);

它只返回 null 以及

alert(error.value);

但这些都不能正常工作。关于如何实现这一目标的任何建议。

标签: angularerror-handlingionic4

解决方案


推荐阅读