可观察到的HttpClient无法从HttpResponse正确映射

时间:2019-04-25 12:35:52

标签: angular typescript httpclient angular7

我在angular7获得了一项服务,这给了我一些帮助:

  getList(
    pageSize: number = 30,
    pageNumber: number = 0,
    filters: CKeyListFilters = <CKeyListFilters>{},
    sortByKey: string = 'activeTo',
    sortOrder: SortOrder = SortOrder.DESC
  ): Observable<ListPaginated<CKey[]>> {
    // ....

    return this.http
      .get<ListPaginated<CKey[]>>(`${environment.CApiUrl}/Get`, {
        params: params,
        observe: 'response',
      })
      .pipe(
        map((resp: HttpResponse<CKey[]>) => {
          return {
            content: resp.body,
            pagination: this.utilities.getPaginationMetaData(resp),
          } as ListPaginated<CKey[]>;
        }),
        catchError((error) => {
          throw error;
        })
      );
  }

但是在pipe方法中,我遇到了这个错误:

error TS2345: Argument of type 'OperatorFunction<HttpResponse<CKey[]>, ListPaginated<CKey[]>>' is not assignable to parameter of type 'OperatorFunction<HttpResponse<ListPaginated<CKey[]>>, ListPaginated<CKey[]>>'.

所以我想将HttpResponse<CKey[]>映射到ListPaginated<CKey[]>,但是我不知道如何转换它。

我继承了这段代码,并且是typescript的新手,所以任何建议对我都有用!

1 个答案:

答案 0 :(得分:1)

好。在这里:

      .get<ListPaginated<CKey[]>>(`${environment.CApiUrl}/Get`, {
        params: params,
        observe: 'response',
      })
      .pipe(
        map((resp: HttpResponse<CKey[]>) => {

第1行中,请求的资源为ListPaginated<CKey[]>类型。在第6行中,map的参数类型为HttpResponse<CKey[]>。这些类型必须匹配(或者您也可以从resp完全删除map的类型)。