簡體   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