Angular 使用模拟数据:Typescript不可分配给类型

Angular 使用模拟数据:Typescript不可分配给类型,angular,typescript,Angular,Typescript,我是Angular的新手,我相信我以错误的方式设置了模拟数据,这会导致以下错误: Argument of type '{ "name": string; "refCode": string; "contentId": string; "enabled": boolean; }[]; } | { ...; }' is not assignable to parameter of type 'Item'. 已更新其他

我是Angular的新手,我相信我以错误的方式设置了模拟数据,这会导致以下错误:

Argument of type '{ "name": string; "refCode": string; "contentId": string; "enabled": boolean;  }[]; } | { ...; }' is not assignable to parameter of type 'Item'.
已更新其他错误消息:

Types of property ... are incompatible.
  Type '{ "name": string; "refCode": string; "contentId": string }[]' is not assignable to type 'Item[]'.
    Property ... is missing in type '{ "name": string; "refCode": string; "contentId": string;}' but required in type 'Item'.
以下是我的json文件内容:

{
  "data": {
    "items": [
      {
        "obj1": "one",
        "obj2": "two",
        "obj3": three
      },
      {
        "obj1": "four",
        "obj2": "five",
        "obj3": siix
        "subCategories": [
          {
          }
        ]
      },
      {
        "name": "Menu Option 1",
        "refCode": "opt1",
        "contentId": "0",
        "subCategories": [
          {}
   
        ]
      },
      {
        "name": "Menu Option 2",
        "refCode": "opt2",
        "contentId": "0"
      },
      {
        "name": "Menu Option 3",
        "refCode": "opt3"
      }
    ]
  }
}
在我的服务类中,我按如下方式导入它:

import dataItems from './shared/my-items.json';
public menuItems: any;
并声明如下:

import dataItems from './shared/my-items.json';
public menuItems: any;
然后我在我的组件类中迭代这里的项目:

const optionData = 
this.itemService.getMenuItems().data.items;
const resource = {};
if (itemData && itemData.length > 0) {
  this.items = [];
  itemData.forEach(element => {
    this.itemService.applyItemName(
      element
    );
    this.items.push(element 
  });
  this.itemService.categories = this.itemss;
}
最后是applyItemName()方法,它为给定项添加项及其子项:

public applyItemName(item: Item) {
if (item.subCategories && item.subCategories.length > 0) {
  item.subCategories.forEach(subCategory => {
    subCategory.parent = category;
    this.applyItemName(subCategory);
  });
}
}

项目对象的模型如下所示:

export class Category {
  public subCategories: Items[];
  public contentId: string;


  constructor(subCategories: Item[], enabled: boolean, name: string) 
  {
      this.subCategories = subCategories;
      this.enabled = enabled;
      this.name = name;
    }
  }

正如我所说的,我是新来的,我不确定我错过了什么。非常感谢任何提示。

我认为存在的问题是
儿童类别
应该是可选的,因为在对象中的某些儿童中,它不存在。在声明中添加
将使其成为可选的:

export class Category {
  public childCategories?: Category[];
或者,您可以始终将childCategories初始化为空数组

  public childCategories: Category[] = [];

错误应该包括一条关于为什么它不可赋值的消息,通常是“缺少参数Foo”之类的。我会把错误信息的缺失部分添加到帖子中。就是这样-谢谢。之前的评论者提醒我其他错误消息,我已经用这些消息更新了原始帖子。非常感谢你。