typescript 如何解决 angular 8 中 Observable 中的捕获错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/58297464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve catch error in Observable in angular 8?
提问by Ashruti
I do error handler code, but I got error in catch. undefined method.
我做了错误处理程序代码,但我在 catch 中出错了。未定义的方法。
This is my serivce.ts code.
这是我的 serivce.ts 代码。
import { Injectable } from "@angular/core";
import { HttpClient, HttpErrorResponse } from "@angular/common/http";
import { user } from "./user";
import { Observable } from "rxjs";
import "rxjs/add/operator/catch";
import "rxjs/add/Observable/throw";
@Injectable({
providedIn: "root"
})
export class DemoService {
private url: string = "/assets/demo/user.json";
constructor(private http: HttpClient) {}
getuserdetails(): Observable<user[]> {
return this.http.get<user[]>(this.url).catch(this.errorHandler);
}
errorHandler(error: HttpErrorResponse) {
return Observable.throw(error.message || "server error.");
}
}
This is my app.component.ts file code
这是我的 app.component.ts 文件代码
public userdetails = [];
public errorMsg;
constructor(private user: DemoService) {}
ngOnInit() {
this.user
.getuserdetails()
.subscribe(
$data => (this.userdetails = $data),
error => (this.errorMsg = error)
);
}
I got error in catch. and error message is Property 'catch' does not exist on type 'Observable'.
我在 catch 中出错了。并且错误消息是“Observable”类型上不存在“catch”属性。
回答by Sudarshana Dayananda
You can use catchErrorin rxjs/operatorsfor this.
Try it as follows.
您可以使用catchError在rxjs/operators此。尝试如下。
import { catchError } from 'rxjs/operators';
export class DemoService {
getuserdetails(): Observable<user[]> {
return this.http.get<user[]>(this.url)
.pipe(catchError(this.errorHandler))
}
errorHandler(error: HttpErrorResponse) {
return Observable.throw(error.message || "server error.");
}
}
回答by Mises
Best way to catch error on observable is:
在 observable 上捕获错误的最佳方法是:
this.http.get<user[]>(this.url).pipe(
tap(),
catchError(err => { return this.errorHandler(err) }
)
If this.http.get()is an Promise lave it like You did in Your code .catch(...)is ok. Try to have catchError(...)at the end of pipe or before finalize(..)if You use it.
如果this.http.get()是一个 Promise,就像你在你的代码中所做的那样.catch(...)。如果您使用它,请尝试catchError(...)在管道末端或之前finalize(..)使用它。
Before Observables had no .pipe()and You where chaining operations like in Promises so they change name .then()to i think flatMap()and .catch()to catchError()So programmer know are it is Observable or Promise.
观测量没有之前.pipe()和你在哪里承诺链接一样操作,使他们改变名称.then(),以我认为flatMap()并.catch()以catchError()这样的程序员都知道它是可观察或承诺。
回答by Ninoos Al Katoni
import { Observable, throwError } from 'rxjs';
import { catchError, } from 'rxjs/operators';
return this.http.get<user[]>(this.url).pipe(catchError(this.errorHandler))
errorHandler(error: HttpErrorResponse) {
return throwError(error.message || "server error.");
}

