角误差TS2339:属性'vehicle‘在'Vehicle[]’类型上不存在。错误发生在data.vehicle.results上。有什么想法吗?模型车有问题吗?我试过在型号不起作用的车型上增加车辆。
1项服务
getVehicles(): Observable<Vehicle[]> {
return this.http.get<Vehicle[]>(CONSTANST.routes.vehicle.list);
}
2组分
getVehicles() {
this.priceruleService.getVehicles()
.pipe(
map(data => {
console.log("data :" , data.vehicle.results)
return data
})
)
.subscribe(data => this.vehicles = data);
}
模型
export interface Vehicle {
_id: number
name: string
Type: string
Stock: string
vehicle: any
}
数据结构
发布于 2019-10-02 23:30:12
您有不是数组的数据,但是您正在返回类型数组的可观察性。让它可以被观察到,然后你就可以访问:
getVehicles(): Observable<Vehicle> {
return this.http.get<Vehicle>(CONSTANST.routes.vehicle.list);
}
发布于 2019-10-02 22:40:07
请试着换衣服
getVehicles() {
this.priceruleService.getVehicles()
.pipe(
map((data:Vehicle) => {
console.log("data :" , data.vehicle.results)
return data
})
)
.subscribe(data => this.vehicles = data);
}
发布于 2019-10-02 22:55:28
如果data
是Vehicle
的数组,那么只需替换
.pipe(
map(data => {
console.log("data :" , data.vehicle.results)
return data
})
)
使用
.pipe(
tap(data => {
console.log("data :" , data);
})
)
属性vehicle
不存在于Vehicle[]
类型(仅在Vehicle
类型上),而且这里没有一个名为results
的属性。
(是的,如果map
获取数据,对其执行副作用,并返回相同的未更改数据,则应该是tap
,而不是map
)。
https://stackoverflow.com/questions/58213356
复制