[英]Property 'object's properties' does not exist on type 'Object'
我有这种代码:
spear.ts:
export class Spears {
constructor(
id: string = null,
transaction_id: string = null,
spear_id: number = null,
delivery_date: any = null,
delivery_time: any = null,
...
) {}
}
稍后,我在我的component.ts类中使用该类,该类将分配有从后端获取的数据
spear.component.ts:
export class CpCoorPenerimaanSpearDetailComponent implements OnInit {
spear = new Spears();
spearForm = new Spears();
constructor() { }
ngOnInit() {
this._spears.getSpearDetail().subscribe(
res => {
this.spear = res.data;
this.spearForm = res.data;
}
err => console.log(err);
);
}
}
每当我想使用对象的值时,它总是显示错误消息,指出该对象的属性不存在Object类型。 例如,当我想在执行spearForm = res.data
之后立即console.log属性时, spearForm = res.data
显示如下消息:
Property 'spear_id' does not exist on type 'Spears'.
我已经为此问题奋斗了5个小时。 我一直在寻找类似的问题,它说只是将属性的类型更改为任何。 事件只是控制台记录的spear_id是数字类型,它表示属性不存在。
使我最困惑的是有时不显示该消息,但是当我感觉一切正常后,再次显示该消息。 有什么帮助吗?
在正确的位置定义类属性
export class Spears {
id: string;
transaction_id: string;
spear_id: number;
delivery_date: any;
delivery_time: any;
constructor(data) {
this.id = data['id'] || null;
this.transaction_id = data['transaction_id'] || null;
// map every field
}
}
然后
this._spears.getSpearDetail().subscribe(
res => {
this.spear = res.data;
this.spearForm = new Spears(res.data);
}
err => console.log(err);
);
希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.