简体   繁体   English

如何在 Angular 8 和类型脚本中为对象和对象数组创建接口?

[英]How to create an interface for object and array of objects in Angular 8 and type script?

I'm trying to create an interface for Object, objects, and array of objects.我正在尝试为对象、对象和对象数组创建一个接口。

Sample interface示例界面

export interface ErateColumn {
  categories:{
    [key:string]:column[]
  }
}

interface column {
  label:string
  value:string
}

Sample API response like this像这样的示例 API 响应

{
"categories": {
    "Basic Information": [
        { label: "Applicant Type", value: "ApplicantType" },
        { label: "Organization Name", value: "OrganizationName" },
    ],
    "FRN Lineitem": [
        { label: "Monthly_Cost", value: "Monthly_Cost" },
        ],
    "FRN status": [
        { label: "Purpose Type", value: "PurposeType" },

    ]
}
}

在此处输入图片说明

what you have provided in this._erateColumn.next(columnList) method is columnList .您在this._erateColumn.next(columnList)方法中提供的是columnList And somehow columnList seems to be a single object of the interface ErateColumn, While your BehaviorSubject has type array of ErateColumn.不知何故, columnList似乎是接口 ErateColumn 的单个对象,而您的 BehaviorSubject 具有 ErateColumn 类型数组。 Change that to ErateColumn or provide array in this._erateColumn.next(columnList) method for your code to work.将其更改为 ErateColumn 或在this._erateColumn.next(columnList)方法中提供数组以使您的代码工作。

EDIT编辑

According to the new screenshot of yours, it says property categories is missing so either make it optional like this根据您的新屏幕截图,它说缺少属性categories ,因此可以像这样将其设为可选

export interface ErateColumn {
  categories?:{
    [key:string]:column[]
  }
}

interface column {
  label:string
  value:string
}

or provide categories.或提供类别。

May be this approach can help someone可能这种方法可以帮助某人

You can convert JSON to typescript's class or interface , Using online tool json2ts or if you are using vs code (Visual Studio) use this extension QuickType - Paste JSON as Code您可以将JSON转换为 typescript 的接口,使用在线工具json2ts或者如果您使用的是vs 代码(Visual Studio),请使用此扩展QuickType - Paste JSON as Code

Note: Your JSON should be a valid JSON otherwise you will get errors注意:你的 JSON 应该是一个有效的 JSON 否则你会得到错误

Hope this helps...希望这可以帮助...

interface User {
   name: string;
   phone: string;
}

const user: User ==> Object

const usersList: User[] ===> Array of Objects

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在角度4中创建类型接口的对象? - How to create object of type interface in angular 4? 使用角度接口创建对象数组 - create array of object using interface in angular 在angular 5中,对象数组的类型返回object。 如何解决这个问题 - In angular 5 Type of array of objects returns object. How to fix this 如何强制从Observable返回的对象类型到Angular中的对象数组? - How to force Object type returned from an Observable to an array of objects in Angular? Angular 2管道:如何创建将对象与对象数组进行比较的管道? - Angular 2 pipes: How to create a pipe that compares an object with an array of objects? 如何在angular中将数组分配给Observable类型的接口 - how to assign an array to an Observable type interface in angular 将对象的 Angular Http 响应转换为接口类型数组 - Transform Angular Http Response from Object of Objects to Interface typed Array 如何使用嵌套数组为 object 创建接口 - How to create interface for object with a nested array 如何将从 webapi 返回的自定义对象数组的 HttpResponse 响应分配给 typescript angular 中相同数组类型的 object? - How to assign an HttpResponse response of custom array of objects returned from webapi to an object of same array type in typescript angular? 如何在 Angular 11 中动态创建对象数组? - How to create array of objects dynamically in Angular 11?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM