Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Angular 8和type script中为对象和对象数组创建接口?_Angular_Typescript - Fatal编程技术网

如何在Angular 8和type script中为对象和对象数组创建接口?

如何在Angular 8和type script中为对象和对象数组创建接口?,angular,typescript,Angular,Typescript,我正在尝试为对象、对象和对象数组创建一个接口 样本接口 export interface ErateColumn { categories:{ [key:string]:column[] } } interface column { label:string value:string } 像这样的API响应示例 { "categories": { "Basic Information": [ { label: "Applicant Type",

我正在尝试为对象、对象和对象数组创建一个接口

样本接口

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

interface column {
  label:string
  value:string
}
像这样的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" },

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

interface column {
  label:string
  value:string
}

您在
中提供的内容。下一个(columnList)
方法是
columnList
。不知何故,
columnList
似乎是接口列的单个对象,而您的BehaviorSubject的类型数组是列。将其更改为列或在
this.\u rateColumn.next(columnList)
方法中提供数组,以使代码正常工作

编辑

根据您的新屏幕截图,它显示属性
类别
缺失,因此要么像这样将其设置为可选

{
"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" },

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

interface column {
  label:string
  value:string
}
或提供类别

也许这种方法可以帮助别人

您可以使用联机工具或使用此扩展插件将JSON转换为typescript的类或接口

注意:您的JSON应该是有效的JSON,否则会出现错误

希望这对您有所帮助

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

const user: User ==> Object

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

#Parth Savaliya,我已经更改为
private readonly _rateColumn=new BehaviorSubject({}as rateColumn)
错误消失了。谢谢你的帮助。我仍然建议您将字段设置为可选字段,而不是使用您的方法