Angular CLI教程

Angular CLI ng lint 命令

本章解释了 ng lint 命令的语法、参数和选项以及一个例子。

语法

ng lint 命令的语法如下-
ng lint <project> [options]
ng l <project> [options]
ng lint 在 angular 应用程序代码上运行 linting 工具。它检查指定的 angular 项目的代码质量。它使用 TSLint 作为默认的 linting 工具,并使用 tslint.json 文件中可用的默认配置。

参数

ng lint 命令的参数如下-
参数和语法 描述
<project> 要 lint 的项目名称。

选项

选项是可选参数。
选项和语法 描述
--configuration=configuration
要使用的 linting 配置。
别名:-c
--exclude 要从 linting 中排除的文件。
--files 要包含在 linting 中的文件。
--fix=true|false 修复 linting 错误(可能会覆盖 linted 文件)。
默认值:false
--force=true|false
即使有 linting 错误也会成功。
默认值:false
--format=format
输出格式(prose、json、stylish、verbose、pmd、msbuild、checkstyle、vso、fileslist)。
默认:prose
--help=true|false|json|JSON
在控制台中显示此命令的帮助消息。
默认值:false
--silent=true|false
显示输出文本。
默认值:false
--tsConfig=tsConfig TypeScript 配置文件的名称。
--tslintConfig=tslintConfig TSLint 配置文件的名称。
--typeCheck=true|false
控制 linting 的类型检查。
默认值:false
首先转到使用 ng build 命令更新的 angular 项目。该命令可在 https://www.lidihuo.com/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>
现在运行 linting 命令。

示例

下面给出了 ng lint 命令的示例-
\>Node\>Lidihuo> ng lint
Linting "Lidihuo"...
ERROR: D:/Node/Lidihuo/src/app/goals/goals.component.ts:9:27-Missing semicolon
ERROR: D:/Node/Lidihuo/src/app/goals/goals.component.ts:13:2-file should end with a newline
Lint errors found in the listed files.
这里 ng lint 命令已经检查了应用程序的代码质量并打印了 linting 状态。
现在更正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 {
   }
}
现在运行 linting 命令。

示例

下面是一个例子-
\>Node\>Lidihuo> ng lint
Linting "Lidihuo"...
All files pass linting.
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4