Angular——理解英雄游的handleError函数

标签 angular typescript rxjs

我正在阅读本教程:
https://angular.io/tutorial/toh-pt6#error-handling

我无法完全理解 error-handling具有以下功能:handleError .

该函数用于此代码段:

/** GET heroes from the server */
getHeroes (): Observable<Hero[]> {
  return this.http.get<Hero[]>(this.heroesUrl)
    .pipe(
      tap(heroes => this.log(`fetched heroes`)),
      catchError(this.handleError('getHeroes', []))
    );
}

这里是 handleError功能:

/**
 * 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);
  };
}

我不明白什么意思:

return (error: any): Observable<T> => { ... }

是不是类似于:

return ((error: any): Observable<T>) => { ... }

我只想知道函数的来源和命运。

您可以提供有关 handleError 逻辑的更多详细信息功能,越多越好。我想深入探讨这一点。

最佳答案

当只有一个参数名时,括号是可选的,您可以从arrow_functions中查看更多详细信息

(param1, param2, …, paramN) => { statements } 
(param1, param2, …, paramN) => expression
// equivalent to: => { return expression; } 

// Parentheses are optional when there's only one parameter name:
(singleParam) => { statements }
singleParam => { statements }

// The parameter list for a function with no parameters should be written with a pair of parentheses.
() => { statements }

通用

这允许我们捕获用户提供的类型,这是 Typescript 的一种类型检查方式,您可以将其称为多种方式之一。

引用


在这个例子中,我们再次使用 T 作为返回类型。通过检查,我们现在可以看到该类型用于返回类型

private handleError<T> (operation = 'operation', result?: T) {
  return (error: any): Observable<T> => {

    ...

    return of(result as T);
  };
}

在下面的示例中,我将解释泛型如何使用 stringnumber 类型

// string type (maybe you want to return some mesage when error)
getHeroes (): Observable<Hero[]> {
  return this.http.get<Hero[]>(this.heroesUrl)
    .pipe(
      ...
      catchError(this.handleError<string>('getHeroes', 'my result....'))
    );
}
// similar to : private handleError<number>(operation = 'operation', result ? : number)
//  return (error: any): Observable <number> => { ...
private handleError<T>(operation = 'operation', result? : T) {
  return (error: any): Observable<T> => {

    ...

    // result: my result....
    return of(result);
  };
}


----

// number type (maybe you want to return ID:1234) when error
getHeroes (): Observable<Hero[]> {
  return this.http.get<Hero[]>(this.heroesUrl)
    .pipe(
      ...
      catchError(this.handleError<string>('getHeroes', '1234'))
    );
}
// similar to : private handleError<string>(operation = 'operation', result ? : string) 
//  return (error: any): Observable <string> => { ...
private handleError<T>(operation = 'operation', result? : T) {
  return (error: any): Observable<T> => {

    ...

    // reuslt: 1234
    return of(result);
  };
}

关于Angular——理解英雄游的handleError函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50997081/

相关文章:

angular - 在 Internet Explorer 11 中使用 Angular Elements 的推荐方法

javascript - 如何在使用 javascript 将数据导出到 .csv 时更改单元格类型和其他属性

typescript - 是否有与 TypeScript 兼容的方式来使用 Sequelize 访问关联实体?

javascript - 列表 : Observable on array map

angular - 如何从 RxJS 映射运算符( Angular )抛出错误

Angular 2 执行选项的方法[elt.selectedIndex].text

angular - 如何从 ag-grid valueFormatter 调用服务

angular - 搜索 ionic 5 中事件的替代方案

Angular 网络包 : Failed to compile

angularjs - RxJS 5 和缓存运算符的替代品