ng generate
基於某個原理圖產生和/或修改檔案。
Generates and/or modifies files based on a schematic.
ng generate <schematic> [options]
ng g <schematic> [options]
引數
引數 | 說明 |
---|---|
<schematic> | 要產生的 schematic 或 collection:schematic The schematic or collection:schematic to generate. 該選項可以接受下列子命令之一: |
選項
選項 | 說明 |
---|---|
--defaults=true|false | 對有預設值的選項禁用互動式輸入提示。 Disable interactive input prompts for options with a default. |
--dry-run=true|false | 執行一遍並彙報其活動軌跡,但不真的寫入任何結果。 Run through and reports activity without writing out results. 預設值: 別名: -d |
--force=true|false | 強制覆蓋現存檔案。 Force overwriting of existing files. 預設值: 別名: -f |
--help= | 在控制檯顯示關於本命令的幫助資訊。 Shows a help message for this command in the console. 預設值: |
--interactive=true|false | 啟用互動式輸入提示。 Enable interactive input prompts. |
Schematic 命令
app-shell
ng generate app-shell [options]
ng g app-shell [options]
為應用的伺服器端版本產生外殼程式。
Generates an app shell for running a server-side version of an app.
選項
選項 | 說明 |
---|---|
--app-dir=app-dir | 應用程式目錄的名稱。 The name of the application directory. 預設值: |
--app-id=app-id | 供 withServerTransition() 使用的應用 ID。 The app ID to use in withServerTransition(). 預設值: |
--client-project=client-project | 相關客戶端應用的名稱。 The name of the related client app. |
--main=main | 主入口點檔案的名稱。 The name of the main entry-point file. 預設值: |
--root-module-class-name= | 根模組類別的名稱。 The name of the root module class. 預設值: |
--root-module-file-name= | 根模組檔案的名稱 The name of the root module file 預設值: |
--route=route | 用於產生應用外殼程式的路由路徑。 Route path used to produce the app shell. 預設值: |
application
ng generate application <name> [options]
ng g application <name> [options]
在工作區的 "projects" 子資料夾中產生新的基本應用定義。
Generates a new basic app definition in the "projects" subfolder of the workspace.
引數
引數 | 說明 |
---|---|
<name> | 新應用的名稱。 The name of the new app. |
選項
選項 | 說明 |
---|---|
--inline-style=true|false | 在根 component.ts 檔案中包含內聯樣式。內聯只能包含 CSS 樣式。預設值為 false,這意味著將在根 component.ts 檔案中建立並參考外部樣式檔案。 Include styles inline in the root component.ts file. Only CSS styles can be included inline. Default is false, meaning that an external styles file is created and referenced in the root component.ts file. 別名: -s |
--inline-template=true|false | 在根 component.ts 檔案中包含內聯範本。預設值為 false,這意味著在根 component.ts 檔案中建立並參考了一個外部範本檔案。 Include template inline in the root component.ts file. Default is false, meaning that an external template file is created and referenced in the root component.ts file. 別名: -t |
--legacy-browsers=true|false | 使用差異載入新增對 Internet Explorer 等舊版瀏覽器的支援。 Add support for legacy browsers like Internet Explorer using differential loading. 預設值: |
--lint-fix=true|false | 已棄用: 改為直接用 "ng lint --fix"。 Use "ng lint --fix" directly instead. 產生此應用程式後,應用 lint 修復程式。 Apply lint fixes after generating the application. |
--minimal=true|false | 建立一個沒有任何測試框架的極簡專案。(僅用於學習目的。) Create a bare-bones project without any testing frameworks. (Use for learning purposes only.) 預設值: |
--prefix=prefix | 用於產生選擇器的字首。 A prefix to apply to generated selectors. 預設值: 別名: -p |
--routing=true|false | 建立一個路由 NgModule。 Create a routing NgModule. 預設值: |
--skip-install=true|false | 跳過安裝相依套件。 Skip installing dependency packages. 預設值: |
--skip-package-json=true|false | 不要將依賴項新增到 “package.json” 檔案中。 Do not add dependencies to the "package.json" file. 預設值: |
--skip-tests=true|false | 不要為應用程式建立 “spec.ts” 測試檔案。 Do not create "spec.ts" test files for the application. 預設值: 別名: -S |
--strict=true|false | 建立具有更嚴格的捆綁套件預算設定的應用程式。 Creates an application with stricter bundle budgets settings. 預設值: |
--style= | 用於樣式檔案的副檔名/前處理器。 The file extension or preprocessor to use for style files. 預設值: |
--view-encapsulation= | 在新應用中使用的檢視封裝策略。 The view encapsulation strategy to use in the new app. |
class
ng generate class <name> [options]
ng g class <name> [options]
在給定或預設專案中建立一個新的通用類別定義。
Creates a new generic class definition in the given or default project.
引數
引數 | 說明 |
---|---|
<name> | 新類別的名稱。 The name of the new class. |
選項
選項 | 說明 |
---|---|
--lint-fix=true|false | 已棄用: 改為直接用 "ng lint --fix"。 Use "ng lint --fix" directly instead. 產生此類別後,應用 lint 修復程式。 Apply lint fixes after generating the class. 預設值: |
--project=project | 專案的名稱。 The name of the project. |
--skip-tests=true|false | 不要為新類別建立 “spec.ts” 測試檔案。 Do not create "spec.ts" test files for the new class. 預設值: |
--type=type | 將開發人員定義的型別新增到檔名中,格式為 “name.type.ts”。 Adds a developer-defined type to the filename, in the format "name.type.ts". 預設值: |
component
ng generate component <name> [options]
ng g component <name> [options]
在給定或預設專案中建立新的通用元件定義。
Creates a new generic component definition in the given or default project.
引數
引數 | 說明 |
---|---|
<name> | 元件的名稱。 The name of the component. |
選項
選項 | 說明 |
---|---|
--change-detection=Default|OnPush | 在新元件中使用的變更檢測策略。 The change detection strategy to use in the new component. 預設值: 別名: -c |
--display-block=true|false | 指定是否要在樣式中包含 Specifies if the style will contain 預設值: 別名: -b |
--entry-component=true|false | 已棄用: 從支援 Ivy 的 9.0.0 版開始,不再需要 entryComponents 了。 Since version 9.0.0 with Ivy, entryComponents is no longer necessary. 此新元件是宣告模組中的入口元件。 The new component is the entry component of the declaring NgModule. 預設值: |
--export=true|false | 將從宣告模組中匯出此元件。 The declaring NgModule exports this component. 預設值: |
--flat=true|false | 在當前專案的最上層建立新檔案。 Create the new files at the top level of the current project. 預設值: |
--inline-style=true|false | 在 component.ts 檔案中包含內聯樣式。只能內聯 CSS 樣式。預設情況下,將建立一個外部樣式檔案,並在 component.ts 檔案中對其進行參考。 Include styles inline in the component.ts file. Only CSS styles can be included inline. By default, an external styles file is created and referenced in the component.ts file. 預設值: 別名: -s |
--inline-template=true|false | 在 component.ts 檔案中包含內聯範本。預設情況下,將建立一個外部範本檔案,並在 component.ts 檔案中對其進行參考。 Include template inline in the component.ts file. By default, an external template file is created and referenced in the component.ts file. 預設值: 別名: -t |
--lint-fix=true|false | 已棄用: 改為直接用 "ng lint --fix"。 Use "ng lint --fix" directly instead. 產生此元件後,應用 lint 修復程式。 Apply lint fixes after generating the component. |
--module=module | 指定其宣告模組。 The declaring NgModule. 別名: -m |
--prefix=prefix | 用於產生元件選擇器的字首。 The prefix to apply to the generated component selector. 別名: -p |
--project=project | 專案的名稱。 The name of the project. |
--selector=selector | 用於此元件的 HTML 選擇器。 The HTML selector to use for this component. |
--skip-import=true|false | 不要將此元件匯入所屬的 NgModule 中。 Do not import this component into the owning NgModule. 預設值: |
--skip-selector=true|false | 指定元件是否應具有選擇器。 Specifies if the component should have a selector or not. 預設值: |
--skip-tests=true|false | 不要為新元件建立 “spec.ts” 測試檔案。 Do not create "spec.ts" test files for the new component. 預設值: |
--style= | 用於樣式檔案的副檔名/前處理器。 The file extension or preprocessor to use for style files. 預設值: |
--type=type | 將開發人員定義的型別新增到檔名中,格式為 “name.type.ts”。 Adds a developer-defined type to the filename, in the format "name.type.ts". 預設值: |
--view-encapsulation= | 在新元件中使用的檢視封裝策略。 The view encapsulation strategy to use in the new component. 別名: -v |
directive
ng generate directive <name> [options]
ng g directive <name> [options]
在給定或預設專案中建立一個新的通用指令定義。
Creates a new generic directive definition in the given or default project.
引數
引數 | 說明 |
---|---|
<name> | 新指令的名稱。 The name of the new directive. |
選項
選項 | 說明 |
---|---|
--export=true|false | 將從宣告模組中匯出此指令。 The declaring NgModule exports this directive. 預設值: |
--flat=true|false | 如果為 true(預設值),則在當前專案的最上層建立新檔案。 When true (the default), creates the new files at the top level of the current project. 預設值: |
--lint-fix=true|false | 已棄用: 改為直接用 "ng lint --fix"。 Use "ng lint --fix" directly instead. 產生此指令後,應用 lint 修復程式。 Apply lint fixes after generating the directive. |
--module=module | 指定其宣告模組。 The declaring NgModule. 別名: -m |
--prefix=prefix | 用於產生選擇器的字首。 A prefix to apply to generated selectors. 別名: -p |
--project=project | 專案的名稱。 The name of the project. |
--selector=selector | 用於此指令的 HTML 選擇器。 The HTML selector to use for this directive. |
--skip-import=true|false | 不要將此指令匯入到所屬的 NgModule 中。 Do not import this directive into the owning NgModule. 預設值: |
--skip-tests=true|false | 不要為新類別建立 “spec.ts” 測試檔案。 Do not create "spec.ts" test files for the new class. 預設值: |
enum
ng generate enum <name> [options]
ng g enum <name> [options]
為給定或預設專案產生新的通用列舉定義。
Generates a new, generic enum definition for the given or default project.
引數
引數 | 說明 |
---|---|
<name> | 列舉的名稱。 The name of the enum. |
選項
選項 | 說明 |
---|---|
--lint-fix=true|false | 已棄用: 改為直接用 "ng lint --fix"。 Use "ng lint --fix" directly instead. 產生此列舉後,應用 lint 修復程式。 Apply lint fixes after generating the enum. |
--project=project | 要在其中建立列舉的專案的名稱。預設是工作區已配置的預設專案。 The name of the project in which to create the enum. Default is the configured default project for the workspace. |
guard
ng generate guard <name> [options]
ng g guard <name> [options]
在給定或預設專案中產生新的通用路由守衛定義。
Generates a new, generic route guard definition in the given or default project.
引數
引數 | 說明 |
---|---|
<name> | 新路由守衛的名稱。 The name of the new route guard. |
選項
選項 | 說明 |
---|---|
--flat=true|false | 如果為 true(預設值),則在當前專案的最上層建立新檔案。 When true (the default), creates the new files at the top level of the current project. 預設值: |
--implements | 指定要實現的介面。 Specifies which interfaces to implement. |
--lint-fix=true|false | 已棄用: 改為直接用 "ng lint --fix"。 Use "ng lint --fix" directly instead. 產生此守衛後,應用 lint 修補程式。 Apply lint fixes after generating the guard. |
--project=project | 專案的名稱。 The name of the project. |
--skip-tests=true|false | 不要為新守衛建立 “spec.ts” 測試檔案。 Do not create "spec.ts" test files for the new guard. 預設值: |
interceptor
ng generate interceptor <name> [options]
ng g interceptor <name> [options]
在給定或預設專案中建立一個新的通用攔截器定義。
Creates a new, generic interceptor definition in the given or default project.
引數
引數 | 說明 |
---|---|
<name> | 攔截器的名稱。 The name of the interceptor. |
選項
選項 | 說明 |
---|---|
--flat=true|false | 如果為 true(預設值),則在專案的最上層建立檔案。 When true (the default), creates files at the top level of the project. 預設值: |
--lint-fix=true|false | 已棄用: 改為直接用 "ng lint --fix"。 Use "ng lint --fix" directly instead. 產生此攔截器後,應用 lint 修復程式。 Apply lint fixes after generating the interceptor. |
--project=project | 專案的名稱。 The name of the project. |
--skip-tests=true|false | 不要為新的攔截器建立 “spec.ts” 測試檔案。 Do not create "spec.ts" test files for the new interceptor. 預設值: |
interface
ng generate interface <name> <type> [options]
ng g interface <name> <type> [options]
在給定或預設專案中建立一個新的通用介面定義。
Creates a new generic interface definition in the given or default project.
引數
引數 | 說明 |
---|---|
<name> | 介面名稱。 The name of the interface. |
<type> | 將開發人員定義的型別新增到檔名中,格式為 “name.type.ts”。 Adds a developer-defined type to the filename, in the format "name.type.ts". |
選項
選項 | 說明 |
---|---|
--lint-fix=true|false | 已棄用: 改為直接用 "ng lint --fix"。 Use "ng lint --fix" directly instead. 產生此介面後,應用 lint 修復程式。 Apply lint fixes after generating the interface. |
--prefix=prefix | 用於產生選擇器的字首。 A prefix to apply to generated selectors. 預設值: |
--project=project | 專案的名稱。 The name of the project. |
library
ng generate library <name> [options]
ng g library <name> [options]
在當前工作空間中建立一個新的通用函式庫專案。
Creates a new generic library project in the current workspace.
引數
引數 | 說明 |
---|---|
<name> | 函式庫的名稱。 The name of the library. |
選項
選項 | 說明 |
---|---|
--entry-file=entry-file | 要建立的函式庫公共 API 檔案的路徑。是相對於工作區根目錄的。 The path at which to create the library's public API file, relative to the workspace root. 預設值: |
--lint-fix=true|false | 已棄用: 改為直接用 "ng lint --fix"。 Use "ng lint --fix" directly instead. 產生此函式庫後,應用 lint 修復程式。 Apply lint fixes after generating the library. |
--prefix=prefix | 用於產生選擇器的字首。 A prefix to apply to generated selectors. 預設值: 別名: -p |
--skip-install=true|false | 不要安裝相依套件。 Do not install dependency packages. 預設值: |
--skip-package-json=true|false | 不要將依賴項新增到 “package.json” 檔案中。 Do not add dependencies to the "package.json" file. 預設值: |
--skip-ts-config=true|false | 不要更新 “tsconfig.json” 來為新函式庫新增路徑對映。在應用中使用該函式庫需要做路徑對映,但是可以在此處將其禁用以簡化開發。 Do not update "tsconfig.json" to add a path mapping for the new library. The path mapping is needed to use the library in an app, but can be disabled here to simplify development. 預設值: |
module
ng generate module <name> [options]
ng g module <name> [options]
在給定或預設專案中建立一個新的通用 NgModule 定義。
Creates a new generic NgModule definition in the given or default project.
引數
引數 | 說明 |
---|---|
<name> | NgModule 的名稱。 The name of the NgModule. |
選項
選項 | 說明 |
---|---|
--flat=true|false | 在當前專案根目錄的最上層建立新檔案。 Create the new files at the top level of the current project root. 預設值: |
--lint-fix=true|false | 已棄用: 改為直接用 "ng lint --fix"。 Use "ng lint --fix" directly instead. 產生此模組後,應用 lint 修復程式。 Apply lint fixes after generating the module. |
--module=module | 指定其宣告模組。 The declaring NgModule. 別名: -m |
--project=project | 專案的名稱。 The name of the project. |
--route=route | 延遲載入模組的路由路徑。提供後,將在新模組中建立一個元件,並將其路由新增到由 The route path for a lazy-loaded module. When supplied, creates a component in the new module, and adds the route to that component in the |
--routing=true|false | 建立一個路由模組。 Create a routing module. 預設值: |
--routing-scope=Child|Root | 新路由模組的範圍。 The scope for the new routing module. 預設值: |
pipe
ng generate pipe <name> [options]
ng g pipe <name> [options]
在給定或預設專案中建立一個新的通用管道定義。
Creates a new generic pipe definition in the given or default project.
引數
引數 | 說明 |
---|---|
<name> | 管道的名稱。 The name of the pipe. |
選項
選項 | 說明 |
---|---|
--export=true|false | 將從宣告模組中匯出此管道。 The declaring NgModule exports this pipe. 預設值: |
--flat=true|false | 如果為 true(預設值),則在專案的最上層建立檔案。 When true (the default) creates files at the top level of the project. 預設值: |
--lint-fix=true|false | 已棄用: 改為直接用 "ng lint --fix"。 Use "ng lint --fix" directly instead. 產生此管道後,應用 lint 修補程式。 Apply lint fixes after generating the pipe. 預設值: |
--module=module | 指定其宣告模組。 The declaring NgModule. 別名: -m |
--project=project | 專案的名稱。 The name of the project. |
--skip-import=true|false | 不要將此管道匯入到所屬的 NgModule 中。 Do not import this pipe into the owning NgModule. 預設值: |
--skip-tests=true|false | 不要為新管道建立 “spec.ts” 測試檔案。 Do not create "spec.ts" test files for the new pipe. 預設值: |
resolver
ng generate resolver <name> [options]
ng g resolver <name> [options]
在給定或預設專案中產生新的通用解析程式定義。
Generates a new, generic resolver definition in the given or default project.
引數
引數 | 說明 |
---|---|
<name> | 新解析器的名稱。 The name of the new resolver. |
選項
選項 | 說明 |
---|---|
--flat=true|false | 如果為 true(預設值),則在當前專案的最上層建立新檔案。 When true (the default), creates the new files at the top level of the current project. 預設值: |
--project=project | 專案的名稱。 The name of the project. |
--skip-tests=true|false | 不要為新的解析器建立 “spec.ts” 測試檔案。 Do not create "spec.ts" test files for the new resolver. 預設值: |
service
ng generate service <name> [options]
ng g service <name> [options]
在給定或預設專案中建立新的通用服務定義。
Creates a new, generic service definition in the given or default project.
引數
引數 | 說明 |
---|---|
<name> | 服務的名稱。 The name of the service. |
選項
選項 | 說明 |
---|---|
--flat=true|false | 如果為 true(預設值),則在專案的最上層建立檔案。 When true (the default), creates files at the top level of the project. 預設值: |
--lint-fix=true|false | 已棄用: 改為直接用 "ng lint --fix"。 Use "ng lint --fix" directly instead. 產生此服務後,應用 lint 修復程式。 Apply lint fixes after generating the service. |
--project=project | 專案的名稱。 The name of the project. |
--skip-tests=true|false | 不要為新服務建立 “spec.ts” 測試檔案。 Do not create "spec.ts" test files for the new service. 預設值: |
service-worker
ng generate service-worker [options]
ng g service-worker [options]
將此原理圖傳遞給 “run” 命令以建立 Service Worker
Pass this schematic to the "run" command to create a service worker
選項
選項 | 說明 |
---|---|
--configuration=configuration | 要應用 Service Worker 的配置。 The configuration to apply service worker to. 預設值: |
--project=project | 專案的名稱。 The name of the project. |
--target=target | 要把 Service Worker 應用到的目標。 The target to apply service worker to. 預設值: |
web-worker
ng generate web-worker <name> [options]
ng g web-worker <name> [options]
在給定或預設專案中建立一個新的通用 Web Worker 定義。
Creates a new generic web worker definition in the given or default project.
引數
引數 | 說明 |
---|---|
<name> | 此 Web Worker 的名字。 The name of the worker. |
選項
選項 | 說明 |
---|---|
--project=project | 專案的名稱。 The name of the project. |
--snippet=true|false | 在同名的同級檔案中新增一個建立 Web Worker 的片段。 Add a worker creation snippet in a sibling file of the same name. 預設值: |
--target=target | 要應用 Web Worker 的目標。 The target to apply web worker to. 預設值: |