Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.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 4服务重写为Angular 7导致Rxjs问题_Angular_Rxjs_Angular7 - Fatal编程技术网

将Angular 4服务重写为Angular 7导致Rxjs问题

将Angular 4服务重写为Angular 7导致Rxjs问题,angular,rxjs,angular7,Angular,Rxjs,Angular7,我很难将angular 4服务重写为angular 7可压缩代码 角度4代码: public load(): Observable<Product[]> { return this.cache<Product[]>(() => this.products, (val: Observable<Product[]>) => this.products = val, () => this.http

我很难将angular 4服务重写为angular 7可压缩代码

角度4代码:

  public load(): Observable<Product[]> {
    return this.cache<Product[]>(() => this.products,
      (val: Observable<Product[]>) => this.products = val,
      () => this.http
        .get(this.apiUrl + "/products")
        .map((response) => response.json()
          .map((item) => {
            let model = new Product();
            model.updateFrom(item);
            return model;
          })));

  }  

我不确定这样做是否正确。查看了文档和其他线程,但找不到答案。请给出您的建议

因为您没有提到您遇到了什么问题,我假设您在映射从HTTP请求返回的结果时遇到了问题

get
请求返回的结果应该是需要转换为
Product
s数组的数组。因此,它应该是:

...

() => this.http.get(this.apiUrl + "/products")
       .pipe(
          map((response: Array<any>) => {

            let result:  Array<Product> = [];

            response.forEach((responseModel) => {
                let model = new Product();
                model.updateFrom(responseModel);
                result.push(model);
            });


            return result;
         })
       )

...
。。。
()=>this.http.get(this.apiUrl+“/products”)
.烟斗(
map((响应:数组)=>{
让结果:数组=[];
response.forEach((responseModel)=>{
让模型=新产品();
model.updateFrom(responseModel);
结果:推送(模型);
});
返回结果;
})
)
...

那么您有什么问题?你没说出什么问题。你会犯什么错误?如果我理解得很好,你只想检查代码?@JosefKatičyes,看看这是不是正确的方法
...

() => this.http.get(this.apiUrl + "/products")
       .pipe(
          map((response: Array<any>) => {

            let result:  Array<Product> = [];

            response.forEach((responseModel) => {
                let model = new Product();
                model.updateFrom(responseModel);
                result.push(model);
            });


            return result;
         })
       )

...