将Angular 4服务重写为Angular 7导致Rxjs问题
我很难将angular 4服务重写为angular 7可压缩代码 角度4代码:将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
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;
})
)
...