@angular-eslint/template/eqeqeq 禁用或允许 null

Rob*_*ste 21 eslint angular typescript-eslint

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

<button [disabled]="someVariable == null"></button>
Run Code Online (Sandbox Code Playgroud)

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

在查看源代码时,我注意到一个名为 的选项allowNullOrUndefined

我尝试像这样定义规则:

"@angular-eslint/template/eqeqeq": [
    "error",
    {
        "allowNullOrUndefined": true
    }
]
Run Code Online (Sandbox Code Playgroud)

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

"@angular-eslint/template/eqeqeq": ["off"]
Run Code Online (Sandbox Code Playgroud)

或者像这样:

"@angular-eslint/template/eqeqeq": "off"
Run Code Online (Sandbox Code Playgroud)

但不幸的是,我仍然在模板中收到错误。

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

dan*_*y74 39

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

{
  "files": [
    "*.html"
  ],
  "extends": [
    "plugin:@angular-eslint/template/recommended"
  ],
  "rules": {
    "@angular-eslint/template/eqeqeq": [
      "error",
      {
        "allowNullOrUndefined": true
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 这是正确的,但我想向未来的观众强调一点:不要将 `*.html` 添加到 `files` 属性中现有的 `*.ts` 中,否则会破坏 TS 文件的 linting。我误解了答案并花了一些时间,直到我意识到下面有一个单独的 html 配置 (3认同)