在Angular 2中用TypeScript给Model和Data添加一个数组

Add an Array to Model and Data in Angular 2 with TypeScript

本文关键字:添加 数组 一个 Data Angular 中用 TypeScript Model      更新时间:2023-09-26

尝试将描述性数组添加到数据和模型时,我得到以下错误。类型"{"id":字符串;"名称":字符串;"能力":字符串;"描述性统计分析":string [];"年":字符串;}[]'不能赋值给类型'Competency[]'。键入'{"id":字符串;"名称":字符串;"能力":字符串;"描述性统计分析":string [];"年":字符串;}'不能赋值给'Competency'类型。属性"描述"的类型是不兼容的。类型"string[]"不能赋值给类型"Descriptive[]"。类型"字符串"不能赋值给类型"描述性"。app/app.routes.ts(9,36):错误TS2307: Cannot find module './inventory/inventory.component'.

mocksCompetency.ts

    import { Competency } from './competency';
    export const COMPETENCY: Competency[] = [
{
    "id": "EngLA001",
    "name": "Oral Language",
    "competency": "The teacher understands the importance of oral language, knows the development processes of oral language and provides the students with varied opportunities to develop listening and speaking skills.",
    "descriptives": ["Something","More"],
    "year": "2014",
}
    ];

competency.ts

    export class Competency {
    id: string;
    name: string;
    competency: string;
    descriptives: Descriptive[];
    year: string;
    }
    export class Descriptive {
        description: string;
    }

你的问题在这里:

"descriptives" : ["Something More"]

根据你的类别,descriptives应该是Descriptive[]类型。Descriptive定义为:

export class Descriptive {
     description: string;
}

这意味着你真正应该做的是:

"descriptives": [{description: "Something More"}]

顺便说一句,对于CompetencyDescriptive,您可能应该使用interface而不是class