【问题标题】:@angular-eslint/template/eqeqeq Disable or allow null@angular-eslint/template/eqeqeq 禁用或允许 null
【发布时间】:2021-10-21 03:18:31
【问题描述】:

使用 Angular 12 和 eslint,我在模板中出现错误,因为我正在这样做:

<button [disabled]="someVariable == null"></button>

预期为===,但收到== eslint(@angular-eslint/template/eqeqeq)

在查看source code 时,我注意到一个名为allowNullOrUndefined 的选项。

我尝试这样定义规则:

"@angular-eslint/template/eqeqeq": [
    "error",
    {
        "allowNullOrUndefined": true
    }
]

但我仍然收到错误消息。所以我尝试完全禁用该规则,如下所示:

"@angular-eslint/template/eqeqeq": ["off"]

或者像这样:

"@angular-eslint/template/eqeqeq": "off"

但没有运气,我仍然在模板中收到错误。

我找不到有关该规则的任何文档。我可以禁用它或允许 null 或 undefined 的 double equal 吗?

【问题讨论】:

  • 使用== null有什么具体原因吗?如果您不关心null,并在someVariable 具有真实值时禁用按钮,您可以简单地执行[disabled]="!someVariable"
  • 好吧....确实:)

标签: angular eslint typescript-eslint


【解决方案1】:

2 年后!你很亲密。将其添加到 .eslintrc.json 的 HTML 部分,如下所示:

{
  "files": [
    "*.html"
  ],
  "extends": [
    "plugin:@angular-eslint/template/recommended"
  ],
  "rules": {
    "@angular-eslint/template/eqeqeq": [
      "error",
      {
        "allowNullOrUndefined": true
      }
    ]
  }
}

显然,请确保您安装了正确的 npm 模块,例如 @angular-eslint/template-parser

如果您使用 ng add @angular-eslint/schematics --skip-confirmation 将 eslint 安装到您的 Angular 项目中,这些应该已经安装。

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 2020-04-19
    • 1970-01-01
    • 2023-02-02
    • 2017-07-24
    • 2017-10-04
    • 2021-12-10
    • 2021-12-09
    • 2018-04-07
    相关资源
    最近更新 更多