我的编码工作正常。数据显示在页面上,但 VSE 编辑器中出现错误
像这样
[ts] Property 'name' does not exist on type 'any[]'.
我的.ts文件代码在这里
ngOnInit() {
const data = {
tailor_id: this.edit_id,
user: this.userToken
};
this.https.post<any>('api/tailor/details', data).subscribe(response => {
this.tailor = response.tailor;
this.editTailorForm.controls['name'].setValue(this.tailor.name);
this.editTailorForm.controls['phone_number'].setValue(this.tailor.phone_number);
});
}
最佳答案
您已经声明了 tailor: any[]
,它是一个数组,因此您应该像 this.tailor[0].name
一样访问它。
如果tailor
是一个对象,则将其声明为tailor:any
并像 this.tailor.name
一样访问。
注意:使用正确的类型(例如 class
、 interface
或类型化对象(例如 tailor : {name])始终是一种良好的做法: string,phone_number: number}
对对象属性进行分组,而不是使用 any
关于javascript - 我的 .ts 代码中显示错误 > [ts] 类型 'name' 上不存在属性 'any[]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54415445/