使用Angular 12 &eslint,我在模板中有一个错误,因为我这样做了:
<button [disabled]="someVariable == null"></button>
期望的
===
但是收到了==
eslint(@angular-eslint/template/eqeqeq)
在浏览源代码时,我注意到一个名为allowNullOrUndefined
的选项。
我试着这样定义这个规则:
"@angular-eslint/template/eqeqeq": [
"error",
{
"allowNullOrUndefined": true
}
]
但是我还是得到了错误。所以我试着完全禁用这个规则,像这样:
"@angular-eslint/template/eqeqeq": ["off"]
或者像这样:
"@angular-eslint/template/eqeqeq": "off"
但是没有运气,我仍然在模板中得到错误。
我找不到任何关于该规则的文档。我可以禁用它或允许双等于null或undefined吗?
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项目中,这些应该已经安装好了。
对于那些想要将规则突出显示为warning
(黄线)而不是error
(红线)的人,只需像这样修改*.html
覆盖对象:
{
"files": ["*.html"],
"extends": ["plugin:@angular-eslint/template/recommended"],
"rules": {
"@angular-eslint/template/eqeqeq": ["warn"]
}
}