Angular 6 return 映射缺失

Angular 6 return mapping missing

正在将项目从 Angular 4 升级到 6,但我的 return 无法正常工作。我的 json 就像

{
    "incomingViolationList":[{"productName": "Mirror",…],
    "incomingViolationListCount": 67
}

我的服务调用看起来像这样,但在 A6 中 .map 不再有效。

return this.http.get('MappViolations/MappViolations?', options)
      .map(response => <GridDataResult>{
        data: response.json().incomingViolationList,
        total: response.json().incomingViolationListCount
      });

我已经开始了我的新服务电话,但不知道如何分成 "data" 和 "total"

return this.http.get<IncommingViolations[]>
      (AppSettings.API_ENDPOINT + 'MappViolations/MappViolations?', { params });

在Angular6中使用了HttpClient而不是Http服务

Using HttpClient the response object is a JSON by default, so there's no need to parse it anymore (response.json() is not needed)

此外,如果您使用 Angular 6 更新更新 RxJS 6,它看起来会像下面的 pipeable 运算符。

return this.http.get('MappViolations/MappViolations?', options)
  .pipe( 
     map((response: any) => ({
      data: response.incomingViolationList,
      total: response..incomingViolationListCount
     })
  );

在 Angular 6 中,您将使用默认 returns json 响应的 HttpClient。因此,您可以从响应中删除 json。

return this.http.get('MappViolations/MappViolations?', options)
     .pipe( 
 map(response => ({
  data: response.incomingViolationList, //<-- remove json
  total: response..incomingViolationListCount //<-- remove json
 })

);

所以您在评论中表示您的情况是 map 不是一个选项。好吧实际上它仍然是,但它的使用方式略有不同

return this.http.get('MappViolations/MappViolations?', options)
      .pipe(
        map(response => <GridDataResult>{
          data: response.json().incomingViolationList,
          total: response.json().incomingViolationListCount
      }));

注意 pipe 直接在 map 的 observable insteed 上的用法。