Angular CLI - ng generate 命令



本章解释了 ng generate 命令的语法、参数和选项,并提供了一个示例。

语法

ng generate 命令的语法如下:

ng generate <schematic> [options]
ng g <schematic> [options]

ng generate 命令根据架构生成和/或修改文件。

参数

ng help 命令的参数如下:

序号 参数 & 语法 描述
1 <schematic> 要生成的架构或集合:架构。此选项可以采用以下子命令之一
  • appShell

  • application

  • class

  • component

  • directive

  • enum

  • guard

  • interceptor

  • interface

  • library

  • module

  • pipe

  • service

  • serviceWorker

  • webWorker

选项

选项是可选参数。

序号 选项 & 语法 描述
1 --defaults=true|false 如果为 true,则禁用具有默认值的选项的交互式输入提示。
2 --dryRun=true|false

如果为 true,则运行并报告活动,而不写入结果。

默认值:false。

别名:-d。

3 --force=true|false

如果为 true,则强制覆盖现有文件。

默认值:false。

别名:-f。

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

在控制台中显示此命令的帮助消息。

默认值:false。

5 --interactive=true|false 如果为 false,则禁用交互式输入提示。

首先移动到使用ng new命令创建的 Angular 项目,然后运行该命令。本章可在https://tutorialspoint.com/angular_cli/angular_cli_ng_new.htm找到。

示例

下面给出了 ng generate 命令的示例:

\>Node\>TutorialsPoint> ng generate component goals
CREATE src/app/goals/goals.component.html (20 bytes)
CREATE src/app/goals/goals.component.spec.ts (621 bytes)
CREATE src/app/goals/goals.component.ts (271 bytes)
CREATE src/app/goals/goals.component.css (0 bytes)
UPDATE src/app/app.module.ts (471 bytes)

在这里,ng generate 命令在我们的 TutorialsPoint 项目中创建了一个新的组件,并在 app.module.ts 中添加了这个新组件的条目。

广告

© . All rights reserved.