如何在TypeScript Angular2中键入数组对象列表

时间:2017-02-12 12:40:48

标签: angular typescript typescript-typings

option.ts

export class Option {
  id_cat: number;
  name_cat: string;
  image_cat: string;
  option_sub : {
    id_sub:  number;
    name_sub: string;
    icon_sub: string;
    ref_id_cat: number;
  }
}

enter image description here

在Json

{"id_cat":4,
"name_cat":"Food",
"image_cat":"","option_sub":[{"id_sub":6,"name_sub":"American Food","icon_sub":"icon ion-pizza","ref_id_cat":4}]}

enter image description here

我想创建MegaMenu

Angular2中的type_sub T_T是什么类型

  option_sub :Array<{
    id_sub:  number;
    name_sub: string;
    icon_sub: string;
    ref_id_cat: number;
  }>;

1 个答案:

答案 0 :(得分:1)

您可以创建另一个Typescript类:

export class Option {
  id_cat: number;
  name_cat: string;
  image_cat: string;
  option_sub : OptionSub[];
}

export class OptionSub {
    id_sub:  number;
    name_sub: string;
    icon_sub: string;
    ref_id_cat: number;
}