在Angular 2中用TypeScript给Model和Data添加一个数组
Add an Array to Model and Data in Angular 2 with TypeScript
尝试将描述性数组添加到数据和模型时,我得到以下错误。类型"{"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"}]
顺便说一句,对于Competency
和Descriptive
,您可能应该使用interface
而不是class
。
相关文章:
- 在函数中添加数组元素的数值
- 在Codrops的内容中添加数组元素展开缩略图网格预览
- 在javascript中动态添加数组数组
- 在 javascript 中添加数组到数组
- 如何使用推送在 JavaScript 中添加数组值
- 设置和添加数组值javascript时出现问题
- 使用 JavaScript 添加数组对象
- 将文件转换为数组并使用数据列表和选项标签为自动完成文本框添加数组元素
- 单击时将值推送到数组,然后循环添加数组值.Javascript
- 如何使用 JavaScript 更新或添加数组中的值
- 在窗体中动态添加数组文本框
- 在 javascript 中添加数组和对象会产生错误
- 如何在JavaScript中更新/添加数组的元素
- 在JavaScript中添加数组中的值
- 通过for循环添加数组值
- 如何在HTML中从Javascript添加数组的滚动列表
- Vaildate动态添加数组输入-jQuery Validate
- 在HighChart android中添加数组数据
- AngularJS:防止再次添加数组中已有的项
- 添加数组元素到现有的javascript数组