繁体   English   中英

空 ngOnInit/Angular

[英]Empty ngOnInit/Angular

在我的 Angular 应用程序中,当我生成新组件时,错误“意外的空构造函数@typescript-eslint/no-empty-function”和“意外的空方法'ngOnInit'@typescript-eslint/no-empty-function”已被触发。

这是我的脚本:

    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 {
        //
      }
    
    }

正如你所看到的,我尝试添加 // ,但问题仍然存在。但是对于页脚和页眉,同样的想法没有问题。有人可以告诉我我错过了什么或者我还能做什么?

这不是编译错误。 Eslint 给出错误迫使你维护代码标准。 如果你想使用 Eslint,但有一些方法可以忽略 'no-empty-function'。 您可以全局禁用错误,或者仅在文件中或仅在构造函数等中禁用错误​​。您可以使用其中任何一个 -

创建 .eslintrc.json 并添加 -

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

此配置全局禁用“无空功能”规则。 您也可以仅在构造函数中允许此规则。 用这个替换之前的规则值 -

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

如果您只是想要特定行的规则,您将添加这一行 -

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

高于你的功能。

暂无
暂无

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

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