简体   繁体   English

Heroes教程中的Angular RXJS CatchError

[英]Angular RXJS CatchError in Heroes tutorial

I'm running the Angular tutorial and I can't understand what is actually happening in one section. 我正在运行Angular教程,但我无法理解一节中实际发生的情况。 I've found some examples from searching, but nothing specifically answers this question. 我从搜索中找到了一些示例,但是没有任何特定答案可以回答这个问题。 Here is the code: 这是代码:

getHeroes (): Observable<Hero[]> {
  return this.http.get<Hero[]>(this.heroesUrl)
    .pipe(
      catchError(this.handleError('getHeroes', []))
    );
} 

Next is the error handler it calls: 接下来是它调用的错误处理程序:

/**
 * Handle Http operation that failed.
 * Let the app continue.
 * @param operation - name of the operation that failed
 * @param result - optional value to return as the observable result
 */
private handleError<T> (operation = 'operation', result?: T) {
  return (error: any): Observable<T> => {

    // TODO: send the error to remote logging infrastructure
    console.error(error); // log to console instead

    // TODO: better job of transforming error for user consumption
    this.log(`${operation} failed: ${error.message}`);

    // Let the app keep running by returning an empty result.
    return of(result as T);
  };
}

I read the documentation I could find on catchError. 我阅读了有关catchError的文档。 I'm new to Typescript and really enjoying it though. 我是Typescript的新手,但真的很喜欢它。 Okay, so the question is why I'm passing a function to catchError, which then returns another function. 好的,问题是为什么我要向catchError传递一个函数,然后catchError返回另一个函数。 Specifically, my question is about the embedded function return (error: any): Observable<T> => { 具体来说,我的问题是关于嵌入式函数的return (error: any): Observable<T> => {

Why is handleError returning a function with fat arrow notation instead of just an observable of type T? 为什么handleError返回带有粗箭头符号的函数,而不是仅返回可观察到的T类型的函数? How is the error parameter of the embedded function receiving data? 嵌入式功能的错误参数如何接收数据?

I think it has something to do with the fact that handleError is called, which returns a function. 我认为这与调用handleError并返回一个函数有关。 So in essence catchError receives that embedded function with the parameter error, but it also has the variables operation and result? 因此,本质上catchError接收带有参数error的嵌入式函数,但是它还具有变量operationresult? in the same scope so it can work with those. 在相同的范围内,因此可以与它们一起使用。 catchError then passes the data to the parameter error and returns an observable T. 然后,catchError将数据传递给参数error并返回一个可观察的T。

RXJS Reference defines catchError as: RXJS参考将catchError定义为:

catchError<T, R>(selector: (err: any, caught: Observable<T>) =>
ObservableInput<R>): OperatorFunction<T, T | R>

But it's hard for me to decipher why it's being passed a function as all the example have. 但是,对于我来说很难解释为什么所有示例都将其传递给函数。

Your assumptions are correct: the handleError function gets called first which itself creates a function for handling the error. 您的假设是正确的:首先调用handleError函数,该函数本身创建一个用于处理错误的函数。 There are a couple of other ways to write this that might help clarify a bit more: 还有其他几种写方法,可能有助于澄清更多内容:

// write the function inline:
catchError(error => {
  console.error(error);
  this.log(`getHeroes failed: ${error.message}`);
  return of([]);
});

// call `handleError` first to get the error handler and pass it as a variable.
const errorHandler = this.handleError('getHeroes', []);
return this.http.get<Hero[]>(this.heroesUrl)
  .pipe(catchError(errorHandler));

catchError requires a function passed to it that returns an Observable to continue the observable stream. catchError需要传递给它的函数 ,该函数返回Observable以继续可观察的流。 The returned observable is created by of . 返回观察到的由创建of The type T allows the error handler to determine the type of the values emitted by the Observable based on the fallback argument you've passed in. 类型T允许错误处理程序根据您传入的后备参数确定Observable发出的值的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM