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.tw/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 中添加了這個新元件的條目。

廣告