this question 的建议答案使用 find 在 Typescript 中不起作用,它无法编译。我看过其他类似的问题,但它们在某种程度上似乎都有些不同(上下文)。
这是数组:
categories: Category[] = [];
这是类别对象:
export class Category{
constructor(
id: string,
name: string,
category_types: Object[]
) {}
}
我正在尝试这样查找(值是一个字符串,例如“Wood”):
let a = this.categories.find(v => v.name === value);
它说“类别”类型上不存在属性名称。
最佳答案
那是因为你的类 Category
没有任何属性。您可以定义 parameter properties直接从构造函数参数创建属性:
export class Category{
constructor(
public id: string,
public name: string,
public category_types: Object[]
) {}
}
现在 Category
的所有参数也是它的公共(public)属性。
关于javascript - Angular2 & Typescript - 类型上不存在属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44095786/