【发布时间】:2019-10-03 15:33:59
【问题描述】:
角度错误 TS2339:“车辆 []”类型上不存在属性“车辆”。错误出现在 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
}
数据结构
【问题讨论】:
-
data长什么样子? -
您正在返回 Vehicle[] 数组,但在访问时使用 data.vehicle。将返回类型更改为普通对象或访问 data[index].vehicle
-
@Manish 对象数组
-
@vipulpatel 先生,您是什么意思,我好像不太清楚
-
@Mr.MarkTawin 那么这可能是您尝试访问对象数组上的
data.vehicle.results的问题。而是使用使用索引来访问对象。因此,如果我没记错,数据实际上是您引用的数组data.vehicle.results
标签: javascript angular typescript