【问题标题】:Identifier 'X' is not defined. 'Y' does not contain such a member未定义标识符“X”。 'Y' 不包含这样的成员
【发布时间】:2021-04-12 14:42:56
【问题描述】:

我有以下接口,我可以将它的参数值传递给基础组件。但是,虽然我可以毫无问题地传递字符串值,但我无法传递布尔值并遇到 "Identifier 'isDisabled' is not defined. 'CustomButton' does not contain such a member" 错误。

export interface CustomButton {
    name: string;
    operation: string;
    tooltip: string;
    isDisabled?: boolean;
}

另一方面,我在一个我称之为基础组件的组件中设置了默认值isDisabled

buttons: CustomButton[];
isEmployeeDisabled = false; // set the value while defining variable

this.buttons = [
  {
    name: 'edit',
    tooltip: 'Edit Employee',
    operation: 'Delete',
    isDisabled: this.isEmployeeDisabled
    // isDisabled: true // if I use like this, it works fine but still gives that error
  }
];  

我找不到为什么 isDisabled 不是已知属性。我该如何解决这个问题?

【问题讨论】:

  • this.buttons: CustomButton[] = [...],试试这个,看看 ide 是否给你错误或警告
  • 谢谢杰克,但我已经使用buttons: CustomButton[]; 定义作为我添加到问题中。所以,我不能像你提到的那样使用它,因为它已经这样定义了。还有其他想法吗?
  • 问题似乎与布尔类型有关,因为其他类型在以相同方式使用时没有问题。
  • 你是对的,我的错。我重新创建了你的场景,我没有收到任何错误,你使用的是哪个版本的 ionic?
  • 离子是什么意思?我使用 Angular 10+

标签: javascript angular components


【解决方案1】:

尝试使用 Stackblitz 时出现此错误:

Type '{name: string; tooltip: string; operation: string; isDisabled: boolean; } 'is not assignable to type' CustomButton '.
Object literal may only specify known properties, and 'operation' does not exist in type 'CustomButton'.

问题是interface 中的operation 属性缺失。

https://stackblitz.com/edit/angular-ngclass-off-props?file=src%2Fapp%2Fapp.component.ts

【讨论】:

  • 感谢回复,但为了清晰起见,我删除了一些行,实际上操作属性在界面中,没有丢失。与该问题相关的任何想法?
猜你喜欢
  • 2018-05-08
  • 2023-03-17
  • 1970-01-01
  • 2021-09-02
  • 2020-07-10
  • 2018-02-18
  • 2013-06-16
  • 2018-09-06
  • 1970-01-01
相关资源
最近更新 更多