【问题标题】:How to declare complex array as property of another property Typescript如何将复杂数组声明为另一个属性 Typescript 的属性
【发布时间】:2017-12-16 17:20:47
【问题描述】:

我对 Angular 和 Typescript 还很陌生,我正在尝试声明一个具有复杂数组的属性作为其属性之一。我的尝试如下所示:

groupedItem: {
     customGroupId: string,
     cgName: string,
     category: [{ 
    customGroupItemId: string,
     name: string }]
 };

当我尝试访问该属性时

this.groupedItem.category.name

我收到一个错误提示

Property 'name' does not exist on type '[{ customGroupItemId: string; name: string; }]'

它看起来就在那里。我究竟做错了什么?

【问题讨论】:

    标签: javascript arrays json angular typescript


    【解决方案1】:

    如果您仔细观察您的对象 groupedItem,您会发现 category 实际上是一个数组而不是对象,因此您需要执行以下操作才能访问它:

    this.groupedItem.category[0].name
    

    【讨论】:

    • 谢谢!做到了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2021-09-19
    • 2021-04-18
    • 1970-01-01
    相关资源
    最近更新 更多