javascript - 我的 .ts 代码中显示错误 > [ts] 类型 'name' 上不存在属性 'any[]'

标签 javascript angular typescript angular6

我的编码工作正常。数据显示在页面上,但 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);
    });

  }

enter image description here

最佳答案

您已经声明了 tailor: any[],它是一个数组,因此您应该像 this.tailor[0].name 一样访问它。

如果tailor是一个对象,则将其声明为tailor:any

并像 this.tailor.name 一样访问。

注意:使用正确的类型(例如 classinterface 或类型化对象(例如 tailor : {name])始终是一种良好的做法: string,phone_number: number} 对对象属性进行分组,而不是使用 any

关于javascript - 我的 .ts 代码中显示错误 > [ts] 类型 'name' 上不存在属性 'any[]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54415445/

相关文章:

javascript - 我的一个 javascript 变量未定义,我该怎么办?

javascript - 如何从当前日期获取过去 12 个月的年份

angular - 可以在 HttpInterceptor 中获取上传进度事件,但不能从 HttpClient 调用中获取吗?

javascript - 从 JavaScript 代码访问 Angular 服务实例

javascript - 避免改变 JavaScript 数组

javascript - JS点击事件监听器没有按预期触发功能

javascript - 从 Google +1 按钮共享时指定图像

json - 输入 "json"| Angular HttpClient 中的 "json"不满足 undefined

typescript - 如何获取Type.length

css - ionic : Sticky button at the bottom of a particular ion-slide