【问题标题】:an array of type Interface接口类型的数组
【发布时间】:2018-08-03 17:16:16
【问题描述】:

我有一个界面如下:

export interface testInterface
{

    value1: string;
    value2:string[];
    SubValue:[{}]

}

我正在填充一个 testInterface 类型的数组——它绑定到一个下拉列表。一切正常。我希望下拉列表将“选择一个”作为其第一个对象。所以我做了以下事情:

default:testInterface[]=[] //==> since the bind array needs to be an array.

this.default.value1="----Please Select the Sub-Industry---";
this.default.value2=[];
this.default.SubValue=[];

我在 subValue 行收到此错误: 类型 'undefined[]' 不能分配给类型 '[{ value1: string;值2:字符串[];价值3...'。 类型“undefined[]”中缺少属性“0”。

为什么我不能说 dropdownArray[0].push=this.default???

有人能解释一下为什么吗?这个问题对你们中的一些人来说可能听起来很明显,但我是新手!

实际代码:

//how I fille the binded array:

 subsDataofSelectedSector:ISubIndustry[]=[];

        if (response.SubIndustries.length > 0) {
          for (i = 0; i < response.SubIndustries.length; i++) {
            this.subsDataofSelectedSector.push(response.SubIndustries[i]);
          }
        }
        
//the interface

export interface ISubIndustry
{

    IndustrySector: string;
    isSelected: string;
    dataSubjectCategories:string[];
    dataTypeCategories:string[];
    SubIndustries:[{}]

}

//the default array

this.defaultSub.dataSubjectCategories =[];
this.defaultSub.dataTypeCategories=[];
this.defaultSub.IndustrySector="----Please Select the Sub-Industry---";
this.defaultSub.SubIndustries=[];
this.defaultSub.isSelected="true";

// i would like to to do the following and add the rest after index 0

 this.subsDataofSelectedSector[0].push(this.defaultSub)

heer 是我必须注入数组的 JSON 文件的一部分。

{
    "IndustrySector":"Other Services Activities",
    "isSelected": "false",
    "dataSubjectCategories":["DS.Employees","DS.Collaborators","DS.Legal Person","DS.Natural Person"],
    "dataTypeCategories":["Personal Data"],
    "SubIndustries": [
      {
        "IndustrySector":"Activities of Membership Organisations",
        "isSelected": "false",
        "dataSubjectCategories":[],
        "dataTypeCategories":[],
        "SubIndustries": [
          {
            "IndustrySector":"Activities of Other Membership Organisations",
            "isSelected": "false",
            "dataSubjectCategories":[],
            "dataTypeCategories":[],
            "SubIndustries":[
              {

如你所见,一个可以有一些子值,每个子也可以有子。所以我将它定义为对象数组

【问题讨论】:

  • 你能显示完整的组件代码吗?
  • 您收到的错误表明 `SubValue:[{}]` 在 {} 之间还有更多内容,您可以发布实际代码吗?
  • 我做了,请看一下
  • 在你的界面中,你为什么不把你的SubValue定义为一个数组而不是一个对象数组?
  • 您的代码还远不是演示错误的最小工作示例。我猜这个 `SubValue:[{}]` 可能应该是SubValue:{}[],因为您可能想要一个数组而不是元组..但很难从你的帖子中说出来

标签: javascript angular typescript mean-stack


【解决方案1】:

default.SubValue 是 Object 类型的数组,即 [Object, Object, Object...] 并且您将未定义的数组分配给值,即 [undefined, undefined],因此打字稿给出了两种类型的错误不一样。您必须指定它是 object 类型才能清除此错误。

this.default.value1="----请选择子行业---";

this.default.value2=[];

this.default.SubValue=[ { } ];

希望这会有所帮助:)

【讨论】:

    【解决方案2】:

    因为你是推入对象,而不是数组。

    此外,SubValue 是一个对象数组而不是数组 this.default.SubValue=[];

    this.default.SubValue.push({});
    

    当您创建接口时,它的属性必须匹配。

    换句话说,如果你的情况(只要所有属性都是必需的,你可以用“问号”将它们设为可选)

    因此,当您初始化默认对象时,其所有属性都必须与接口匹配,例如

    default.push({
      value1 = "----Please Select the Sub-Industry---",
      value2 = [],
      SubValue = [{}]
    })
    

    或者制作可选属性并使用你的方式

    export interface testInterface
    {
    
        value1: string;
        value2?:string[];
        SubValue?:[{}]
    
    }
    

    总结一下...在您的情况下,问题出在 this.default.value1 这一行

    1. 默认值必须是一个值
    2. 其属性必须与接口中的定义相匹配

    【讨论】:

    • 这表示:类型 'undefined[]' 不能分配给类型 '[{}]'
    • 改变了答案。看看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 2021-01-22
    • 1970-01-01
    • 2012-11-04
    • 2018-09-15
    • 2015-12-02
    • 2016-02-22
    相关资源
    最近更新 更多