【问题标题】:Add an Array to Model and Data in Angular 2 with TypeScript使用 TypeScript 在 Angular 2 中将数组添加到模型和数据
【发布时间】:2017-03-20 06:17:51
【问题描述】:

尝试将描述性数组添加到我的数据和模型时出现以下错误。 错误 TS2322:类型'{“id”:字符串; “名称”:字符串; “能力”:字符串; “描述”:字符串[]; “年”:字符串; }[]' 不可分配给类型 'Competency[]'。 输入'{“id”:字符串; “名称”:字符串; “能力”:字符串; “描述”:字符串[]; “年”:字符串; }' 不可分配给类型“能力”。 属性“描述”的类型不兼容。 类型 'string[]' 不能分配给类型 'Descriptive[]'。 类型“字符串”不可分配给类型“描述性”。 app/app.routes.ts(9,36): 错误 TS2307: 找不到模块 './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",
}
    ];

能力.ts

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

【问题讨论】:

    标签: javascript angularjs typescript model


    【解决方案1】:

    你的问题在这里:

    "descriptives" : ["Something More"]
    

    根据您的班级,descriptives 应该是 Descriptive[] 类型。而Descriptive定义为:

    export class Descriptive {
         description: string;
    }
    

    这意味着你真的应该这样做:

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

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

    【讨论】:

    • 感谢您的回复。非常有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 2017-07-10
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多