angular - 向我们的用户显示可观察到的错误消息
问题描述
我在 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);
但这些都不能正常工作。关于如何实现这一目标的任何建议。
解决方案
推荐阅读
- c# - 自托管 ASP.NET Core Web API 导致错误 - 无法建立连接,因为目标计算机主动拒绝它
- android - android webview 显示错误“无法加载应用程序。此应用程序不安全”
- python - 解析 Beautiful Soup 上具有相同类的所有元素
- elasticsearch - Elasticsearch - 没有第二次重新索引的零停机时间重新索引?
- python - 标签文本下的Python抓取?
- python - 单击图像 1 次,获取位置并销毁窗口 OpenCv
- ios - iOS 13 - UIApplicationDelegate 中的 applicationWillTerminate 等效于 UISceneDelegate?
- python - 每个键将几乎相同的字典值列表组合在一起
- python - 无法生成任何 Kafka 消息
- powershell - 运行 Remote PS 脚本以部署软件