代码之家  ›  专栏  ›  技术社区  ›  LakiGeri

HttpClint的Observable无法从HttpResponse正确映射

  •  0
  • LakiGeri  · 技术社区  · 7 年前

    我在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 ,所以任何建议都对我有用!

    0 回复  |  直到 7 年前
        1
  •  1
  •   mbojko    7 年前

    好啊这是:

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

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

    推荐文章