简体   繁体   English

空 ngOnInit/Angular

[英]Empty ngOnInit/Angular

In my Angular application,when i generated new components the error "Unexpected empty constructor @typescript-eslint/no-empty-function" and "Unexpected empty method 'ngOnInit' @typescript-eslint/no-empty-function" has been triggred.在我的 Angular 应用程序中,当我生成新组件时,错误“意外的空构造函数@typescript-eslint/no-empty-function”和“意外的空方法'ngOnInit'@typescript-eslint/no-empty-function”已被触发。

This is my script:这是我的脚本:

    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'app-header',
      templateUrl: './header.component.html',
      styleUrls: ['./header.component.sass']
    })
    export class HeaderComponent implements OnInit {
    
      constructor() { 
        //
       }
    
      ngOnInit(): void {
        //
      }
    
    }

As you can see i tried to add // ,but the problem still persists.But for footer and header the same idea worked without problems.Can someone tell me what I am missing or what else i can do?正如你所看到的,我尝试添加 // ,但问题仍然存在。但是对于页脚和页眉,同样的想法没有问题。有人可以告诉我我错过了什么或者我还能做什么?

This is not a compilation error.这不是编译错误。 Eslint gives the error to force you to maintain code standards. Eslint 给出错误迫使你维护代码标准。 If you want to use Eslint, But ignore 'no-empty-function' there are some ways.如果你想使用 Eslint,但有一些方法可以忽略 'no-empty-function'。 You can globally disable the error, or only in a file or only in a constructor etc. You can use any of them -您可以全局禁用错误,或者仅在文件中或仅在构造函数等中禁用错误​​。您可以使用其中任何一个 -

create .eslintrc.json there add -创建 .eslintrc.json 并添加 -

{
  "root": true,
  "overrides": [
    {
      "files": ["*.ts"],
      "rules": {
        "@typescript-eslint/no-empty-function": "off"
      }
    }
  ]
}

this config globally disables 'no-empty-function' rule.此配置全局禁用“无空功能”规则。 You can also allow this rule only in constructor.您也可以仅在构造函数中允许此规则。 Replace the previous rules value with this -用这个替换之前的规则值 -

"rules":{
   "@typescript-eslint/no-empty-function":[
      "error",
      {
         "allow":[
            "constructors"
         ]
      }
   ]
}

If you simply want the rule for the specific line you will add this line -如果您只是想要特定行的规则,您将添加这一行 -

 // eslint-disable-next-line @typescript-eslint/no-empty-function

above your functions.高于你的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM