我有一个服务,它返回一个可观察的,在得到响应后,我映射响应并将结果分配给数组。
this.dataService.fetchData().subscribe(response =>{
[...this.DataMappingField ]= response.map(({ ID: id, Name: name }) => ({ id, name }));
});通过订阅内部的扩展操作符,我得到了下面的错误。
错误:'Modelclass'.类型上不存在属性'map‘
服务部门的反应
[
{ID: 6197, Name: 'A', desc: null,catergory:'lap'},
{ID: 6198, Name: 'B', desc: null,catergory:'lap'}
]在服务中,fetchData方法:
fetchData(): Observable<Modelclass> {
return this.http
.get<Modelclass>(REQUEST_URL)
.pipe(
tap(response => response)
);
}模型类:
export class Modelclass {
ID: any;
Name: string;
}但是,下面的代码在使用方法调用时运行良好:
this.dataService.fetchData().subscribe(this.fetchResponse);
private fetchResponse(response): void => {
[...this.DataMappingField ]= response.map(({ ID: id, Name: name }) => ({ id, name }));
}发布于 2018-09-07 17:32:08
我认为问题是不正确的响应输入。fetchData方法的定义应该如下:
fetchData(): Observable<Modelclass[]> {
return this.http
.get<Modelclass[]>(REQUEST_URL)
.pipe(
tap(response => response)
);
}注意,我使用Modelclass[]作为响应类型。
https://stackoverflow.com/questions/52171868
复制相似问题