Angular CLI - ng lint 命令



本章解釋了 ng lint 命令的語法、引數和選項,並提供了一個示例。

語法

ng lint 命令的語法如下:

ng lint <project> [options]
ng l <project> [options]

ng lint 在 Angular 應用程式碼上執行 lint 工具。它檢查指定 Angular 專案的程式碼質量。它預設使用 TSLint 作為 lint 工具,並使用 tslint.json 檔案中提供的預設配置。

引數

ng lint 命令的引數如下:

序號 引數 & 語法 描述
1 <project> 要 lint 的專案的名稱。

選項

選項是可選引數。

序號 選項 & 語法 描述
1 --configuration=configuration

要使用的 lint 配置。

別名: -c

2 --exclude 要從 lint 中排除的檔案。
3 --files 要包含在 lint 中的檔案。
4 --fix=true|false 修復 lint 錯誤(可能會覆蓋已 lint 的檔案)。

預設值: false

5 --force=true|false

即使存在 lint 錯誤也成功。

預設值: false

6 --format=format

輸出格式(prose、json、stylish、verbose、pmd、msbuild、checkstyle、vso、fileslist)。

預設值: prose

7 --help=true|false|json|JSON

在控制檯中顯示此命令的幫助訊息。

預設值: false

8 --silent=true|false

顯示輸出文字。

預設值: false

9 --tsConfig=tsConfig TypeScript 配置檔案的名稱。
10 --tslintConfig=tslintConfig TSLint 配置檔案的名稱。
11 --typeCheck=true|false

控制 lint 的型別檢查。

預設值: false

首先移動到使用 **ng build** 命令更新的 Angular 專案。該命令可在 https://tutorialspoint.tw/angular_cli/angular_cli_ng_build.htm 找到。

按如下方式更新 goals.component.html 和 goals.component.ts:

goals.component.ts

import { Component, OnInit } from '@angular/core';
@Component({
   selector: 'app-goals',
   templateUrl: './goals.component.html',
   styleUrls: ['./goals.component.css']
})
export class GoalsComponent implements OnInit {
   title = 'Goal Component'
   constructor() { }
   ngOnInit(): void {
   }
}

goals.component.html

<p>{{title}}</p>

現在執行 lint 命令。

示例

下面給出了 ng lint 命令的示例:

\>Node\>TutorialsPoint> ng lint
Linting "TutorialsPoint"...
ERROR: D:/Node/TutorialsPoint/src/app/goals/goals.component.ts:9:27 - Missing semicolon
ERROR: D:/Node/TutorialsPoint/src/app/goals/goals.component.ts:13:2 - file should end with a newline
Lint errors found in the listed files.

這裡 ng lint 命令檢查了應用程式的程式碼質量並列印了 lint 狀態。

現在更正 goals.component.ts 中的錯誤。

goals.component.ts

import { Component, OnInit } from '@angular/core';
@Component({
   selector: 'app-goals',
   templateUrl: './goals.component.html',
   styleUrls: ['./goals.component.css']
})
export class GoalsComponent implements OnInit {
   title = 'Goal Component';
   constructor() { }
   ngOnInit(): void {
   }
}

現在執行 lint 命令。

示例

下面給出一個示例:

\>Node\>TutorialsPoint> ng lint
Linting "TutorialsPoint"...
All files pass linting.
廣告