ESLint 抱怨空构造函数和 ngOnInit 实现
ESLint complaining about empty constructor and ngOnInit-implementation
在我的 Angular 应用程序中,规则 no-empty-function 为我的构造函数触发了错误。好吧,它 确实是 一个空体,但构造函数本身需要在那里,因为我注入了一个服务。
export class ClientListComponent implements OnInit {
constructor(service: AuxService) { }
ngOnInit() { }
...
}
有趣的是,它还抱怨接口的实现方法。但是,这两者之间的错误消息各不相同,这让我很困惑。
Unexpected empty constructor.
Unexpected empty method 'ngOnInit'.
因此,它清楚地区分了普通方法(由于实现的接口仍然需要一个方法,在我看来唠叨这本身就是错误的)和构造函数。我相信创作者都听说过依赖注入,所以我不明白我错过了什么。
我想你需要检查下面link
https://eslint.org/docs/rules/no-empty-function
根据文档,以下内容不正确
constructor() {}
foo() {}
这些都是正确的
constructor() {
// do nothing.
}
foo() {
// do nothing.
}
对于构造函数,您可以使用此设置:{ "allow": ["functions"] }
。但是此时ngOnInit
,除了去掉方法和implements OnInit
.
,没有别的办法了
您还可以修改“.eslintrc.json”文件以从 linter 中排除该规则。
规则其实是组合的,不是只有一个:
"no-empty-function": "off",
"@typescript-eslint/no-empty-function": "off",
"@angular-eslint/no-empty-lifecycle-method": "off"
我把上面给出的三个规则添加到.eslintrc.json[=之后,我才摆脱了所有的空函数错误24=] 文件.
复制这三个规则,将它们粘贴到您的 .eslintrc.json 文件中(在“规则” 中),然后你会摆脱错误。
您需要编辑 .eslintrc.json 文件,并添加此行
"@angular-eslint/no-empty-lifecycle-method": "off"
或查看此文件
{
"root": true,
"ignorePatterns": ["projects/**/*"],
"overrides": [
{
"files": ["*.ts"],
"parserOptions": {
"project": ["tsconfig.json"],
"createDefaultProgram": true
},
"extends": [
"plugin:@angular-eslint/recommended",
"plugin:@angular-eslint/template/process-inline-templates"
],
"rules": {
"@angular-eslint/directive-selector": [
"error",
{
"type": "attribute",
"prefix": "app",
"style": "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
"type": "element",
"prefix": "app",
"style": "kebab-case"
}
],
"@angular-eslint/no-empty-lifecycle-method": "off"
}
},
{
"files": ["*.html"],
"extends": ["plugin:@angular-eslint/template/recommended"],
"rules": {}
}
]
}
在我的 Angular 应用程序中,规则 no-empty-function 为我的构造函数触发了错误。好吧,它 确实是 一个空体,但构造函数本身需要在那里,因为我注入了一个服务。
export class ClientListComponent implements OnInit {
constructor(service: AuxService) { }
ngOnInit() { }
...
}
有趣的是,它还抱怨接口的实现方法。但是,这两者之间的错误消息各不相同,这让我很困惑。
Unexpected empty constructor.
Unexpected empty method 'ngOnInit'.
因此,它清楚地区分了普通方法(由于实现的接口仍然需要一个方法,在我看来唠叨这本身就是错误的)和构造函数。我相信创作者都听说过依赖注入,所以我不明白我错过了什么。
我想你需要检查下面link https://eslint.org/docs/rules/no-empty-function
根据文档,以下内容不正确
constructor() {}
foo() {}
这些都是正确的
constructor() {
// do nothing.
}
foo() {
// do nothing.
}
对于构造函数,您可以使用此设置:{ "allow": ["functions"] }
。但是此时ngOnInit
,除了去掉方法和implements OnInit
.
您还可以修改“.eslintrc.json”文件以从 linter 中排除该规则。
规则其实是组合的,不是只有一个:
"no-empty-function": "off",
"@typescript-eslint/no-empty-function": "off",
"@angular-eslint/no-empty-lifecycle-method": "off"
我把上面给出的三个规则添加到.eslintrc.json[=之后,我才摆脱了所有的空函数错误24=] 文件.
复制这三个规则,将它们粘贴到您的 .eslintrc.json 文件中(在“规则” 中),然后你会摆脱错误。
您需要编辑 .eslintrc.json 文件,并添加此行
"@angular-eslint/no-empty-lifecycle-method": "off"
或查看此文件
{
"root": true,
"ignorePatterns": ["projects/**/*"],
"overrides": [
{
"files": ["*.ts"],
"parserOptions": {
"project": ["tsconfig.json"],
"createDefaultProgram": true
},
"extends": [
"plugin:@angular-eslint/recommended",
"plugin:@angular-eslint/template/process-inline-templates"
],
"rules": {
"@angular-eslint/directive-selector": [
"error",
{
"type": "attribute",
"prefix": "app",
"style": "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
"type": "element",
"prefix": "app",
"style": "kebab-case"
}
],
"@angular-eslint/no-empty-lifecycle-method": "off"
}
},
{
"files": ["*.html"],
"extends": ["plugin:@angular-eslint/template/recommended"],
"rules": {}
}
]
}