[英]Property 'object's properties' does not exist on type 'Object'
I have this kind of code: 我有这种代码:
spear.ts: 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,
...
) {}
}
Later on, I use that class in my component.ts class and it will be assigned with data fetched from the back-end 稍后,我在我的component.ts类中使用该类,该类将分配有从后端获取的数据
spear.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);
);
}
}
Everytime I want to use the value of the object it always showing error message that the property of the object does not exist of type Object. 每当我想使用对象的值时,它总是显示错误消息,指出该对象的属性不存在Object类型。 For example when I want to console.log the property just after executing
spearForm = res.data
it just show message like this: 例如,当我想在执行
spearForm = res.data
之后立即console.log属性时, spearForm = res.data
显示如下消息:
Property 'spear_id' does not exist on type 'Spears'.
I have been struggling for 5 hours for this problem. 我已经为此问题奋斗了5个小时。 I have looked for similar question and It said just change the type of the property to any.
我一直在寻找类似的问题,它说只是将属性的类型更改为任何。 Event just console logging the spear_id which is in type of number, it said property does not exist.
事件只是控制台记录的spear_id是数字类型,它表示属性不存在。
The thing that make me confused the most is sometimes the message is not shown, but after I feel everything going well, the message shown again. 使我最困惑的是有时不显示该消息,但是当我感觉一切正常后,再次显示该消息。 Is there any help?
有什么帮助吗?
Define class properties in the correct place 在正确的位置定义类属性
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
}
}
Then 然后
this._spears.getSpearDetail().subscribe(
res => {
this.spear = res.data;
this.spearForm = new Spears(res.data);
}
err => console.log(err);
);
Hope this will help! 希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.