Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何在NG6服务中应用过滤器,在6之前的版本中工作_Angular_Typescript_Rxjs - Fatal编程技术网

Angular 如何在NG6服务中应用过滤器,在6之前的版本中工作

Angular 如何在NG6服务中应用过滤器,在6之前的版本中工作,angular,typescript,rxjs,Angular,Typescript,Rxjs,这个方法在使用Http时工作得很好,但我想消除关于它被弃用的警告。我导入了HttpClient并将其添加到构造函数中。我知道我不再需要json(),因为数据现在默认为json。但我得到一个错误,类型对象上不存在过滤器。如何重建此GetPatchGroup import { Injectable } from '@angular/core'; import { environment } from '../../../environments/environment'; import { Htt

这个方法在使用Http时工作得很好,但我想消除关于它被弃用的警告。我导入了HttpClient并将其添加到构造函数中。我知道我不再需要json(),因为数据现在默认为json。但我得到一个错误,类型对象上不存在过滤器。如何重建此GetPatchGroup

import { Injectable } from '@angular/core';
import { environment } from '../../../environments/environment';

import { Http, Response } from '@angular/http';
import { HttpClient } from '@angular/common/http';
import { catchError, filter, map, mergeMap, partition, retryWhen } from 'rxjs/operators';
import { Observable, Subject, of, pipe, range, throwError, timer, zip } from 'rxjs';

import { PatchGroup, Server } from '../../shared/models/patchgroup.models';

const API_URL = environment.apiUrl;

@Injectable( {
  providedIn: 'root'
})
export class AppsApiService {

  constructor(private http: HttpClient) { }

  getPatchGroups(appId: string): Observable<{ ungrouped: PatchGroup[], grouped: PatchGroup[] }> {
    return this.http
      .get(API_URL + 'PatchGroups/ForApp/' + appId)
      .pipe(
      map(data => {
        return { ungrouped: data.json().filter(d => d.id < 0), grouped: data.json().filter(d => d.id > 0) };
        }),
        catchError(this.handleError)
      );
  }

  private handleError(error: Response | any) {
    console.error('AppsApiService::handleError', error);
    return Observable.throw(error.message || 'Server error');
  }
}
从'@angular/core'导入{Injectable};
从“../../../environments/environment”导入{environment};
从'@angular/Http'导入{Http,Response};
从'@angular/common/http'导入{HttpClient};
从“rxjs/operators”导入{catchError、filter、map、mergeMap、partition、retryWhen};
从“rxjs”导入{可观察、主题、of、管道、范围、投掷者、计时器、zip};
从“../../shared/models/PatchGroup.models”导入{PatchGroup,Server};
const API_URL=environment.apirl;
@可注射({
providedIn:'根'
})
导出类AppsApiService{
构造函数(私有http:HttpClient){}
getPatchGroups(appId:string):可观察{
返回此文件。http
.get(API_URL+'PatchGroups/ForApp/'+appId)
.烟斗(
地图(数据=>{
返回{ungroup:data.json().filter(d=>d.id<0),group:data.json().filter(d=>d.id>0)};
}),
catchError(this.handleError)
);
}
私有句柄错误(错误:响应|任意){
console.error('AppsApiService::handleError',error);
返回Observable.throw(error.message | |'服务器错误');
}
}

data.json()
返回一个普通的JS对象,而不是一个可观察的对象,因此您尝试使用
数组。filter
可能在
对象上,其中
filter
不存在。我知道filter不存在,所以我现在把它放在哪里?或者我可以使用HttpClient进行过滤吗?首先,检查
data.json()
返回的内容,因为您可能认为它返回数组,但实际上它没有返回。您不能以这种方式使用过滤器,原因有二。首先,你在一个物体上使用它,而不可观察。第二个-你的过滤器有两个条件,不能通过过滤观测值来满足。为什么不在成功回调时返回所有内容并进行过滤呢?对于旧的Http,数据作为响应返回。现在使用HttpClient,数据是一个对象。