ng generate

Generates and/or modifies files based on a schematic.

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

参数

参数说明
schematic

The schematic or collection:schematic to generate.

This option can take one of the following sub-commands:

选项

选项说明
--defaults=true|false --defaults|--noDefaults

When true, disables interactive input prompts for options with a default.

--dryRun=true|false --dryRun|--noDryRun
-d=true|false -d|-noD

When true, run through and report activity without writing out results.

--force=true|false --force|--noForce
-f=true|false -f|-noF

When true, force overwriting of existing files.

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

Shows a help message for this command in the console.

--interactive=true|false --interactive|--noInteractive

When false, disables interactive input prompts.

Schematic 命令

appShell

ng generate appShell [options]ng g appShell [options]

选项

选项说明
--appDir=appDir

The name of the application directory.

--appId=appId

The appId to use withServerTransition.

--clientProject=clientProject

Name of related client app.

--index=index

Name of the index file

--main=main

The name of the main entry-point file.

--name=name

Name of the universal app

--outDir=outDir

The output directory for build results.

--root=root

The root directory of the app.

--rootModuleClassName=rootModuleClassName

The name of the root module class.

--rootModuleFileName=rootModuleFileName

The name of the root module file

--route=route

Route path used to produce the app shell.

--sourceDir=sourceDir
-D=sourceDir

The path of the source directory.

--test=test

The name of the test entry-point file.

--testTsconfigFileName=testTsconfigFileName

The name of the TypeScript configuration file for tests.

--tsconfigFileName=tsconfigFileName

The name of the TypeScript configuration file.

--universalProject=universalProject

Name of related universal app.

application

ng generate application <name> [options]ng g application <name> [options]

参数

参数说明
name

The name of the application.

选项

选项说明
--experimentalIvy=true|false --experimentalIvy|--noExperimentalIvy

EXPERIMENTAL: Specifies whether to create a new application which uses the Ivy rendering engine.

--inlineStyle=true|false --inlineStyle|--noInlineStyle
-s=true|false -s|-noS

Specifies if the style will be in the ts file.

--inlineTemplate=true|false --inlineTemplate|--noInlineTemplate
-t=true|false -t|-noT

Specifies if the template will be in the ts file.

--minimal=true|false --minimal|--noMinimal

Create a barebones project without any testing frameworks

--prefix=prefix
-p=prefix

The prefix to apply to generated selectors.

--routing=true|false --routing|--noRouting

Generates a routing module.

--skipPackageJson=true|false --skipPackageJson|--noSkipPackageJson

Do not add dependencies to package.json.

--skipTests=true|false --skipTests|--noSkipTests
-S=true|false -S|-noS

Skip creating spec files.

--style=style

The file extension to be used for style files.

--viewEncapsulation=Emulated|Native|None|ShadowDom

Specifies the view encapsulation strategy.

class

ng generate class <name> [options]ng g class <name> [options]

参数

参数说明
name

The name of the class.

选项

选项说明
--project=project

The name of the project.

--spec=true|false --spec|--noSpec

Specifies if a spec file is generated.

--type=type

Specifies the type of class.

component

ng generate component <name> [options]ng g component <name> [options]

参数

参数说明
name

The name of the component.

选项

选项说明
--changeDetection=Default|OnPush
-c=Default|OnPush

Specifies the change detection strategy.

--entryComponent=true|false --entryComponent|--noEntryComponent

Specifies if the component is an entry component of declaring module.

--export=true|false --export|--noExport

Specifies if declaring module exports the component.

--flat=true|false --flat|--noFlat

Flag to indicate if a dir is created.

--inlineStyle=true|false --inlineStyle|--noInlineStyle
-s=true|false -s|-noS

Specifies if the style will be in the ts file.

--inlineTemplate=true|false --inlineTemplate|--noInlineTemplate
-t=true|false -t|-noT

Specifies if the template will be in the ts file.

--lintFix=true|false --lintFix|--noLintFix

Specifies whether to apply lint fixes after generating the component.

--module=module
-m=module

Allows specification of the declaring module.

--prefix=prefix
-p=prefix

The prefix to apply to generated selectors.

--project=project

The name of the project.

--selector=selector

The selector to use for the component.

--skipImport=true|false --skipImport|--noSkipImport

Flag to skip the module import.

--spec=true|false --spec|--noSpec

Specifies if a spec file is generated.

--styleext=styleext

The file extension to be used for style files.

--viewEncapsulation=Emulated|Native|None|ShadowDom
-v=Emulated|Native|None|ShadowDom

Specifies the view encapsulation strategy.

directive

ng generate directive <name> [options]ng g directive <name> [options]

参数

参数说明
name

The name of the directive.

选项

选项说明
--export=true|false --export|--noExport

Specifies if declaring module exports the directive.

--flat=true|false --flat|--noFlat

Flag to indicate if a dir is created.

--lintFix=true|false --lintFix|--noLintFix

Specifies whether to apply lint fixes after generating the directive.

--module=module
-m=module

Allows specification of the declaring module.

--prefix=prefix
-p=prefix

The prefix to apply to generated selectors.

--project=project

The name of the project.

--selector=selector

The selector to use for the directive.

--skipImport=true|false --skipImport|--noSkipImport

Flag to skip the module import.

--spec=true|false --spec|--noSpec

Specifies if a spec file is generated.

enum

ng generate enum <name> [options]ng g enum <name> [options]

参数

参数说明
name

The name of the enum.

选项

选项说明
--lintFix=true|false --lintFix|--noLintFix

Specifies whether to apply lint fixes after generating the enum.

--project=project

The name of the project.

guard

ng generate guard <name> [options]ng g guard <name> [options]

参数

参数说明
name

The name of the guard.

选项

选项说明
--flat=true|false --flat|--noFlat

Flag to indicate if a dir is created.

--lintFix=true|false --lintFix|--noLintFix

Specifies whether to apply lint fixes after generating the guard.

--project=project

The name of the project.

--spec=true|false --spec|--noSpec

Specifies if a spec file is generated.

interface

ng generate interface <name> <type> [options]ng g interface <name> <type> [options]

参数

参数说明
name

The name of the interface.

type

Specifies the type of interface.

选项

选项说明
--lintFix=true|false --lintFix|--noLintFix

Specifies whether to apply lint fixes after generating the directive.

--prefix=prefix

Specifies the prefix to use.

--project=project

The name of the project.

library

ng generate library <name> [options]ng g library <name> [options]

参数

参数说明
name

The name of the library.

选项

选项说明
--entryFile=entryFile

The path to create the library's public API file.

--prefix=prefix
-p=prefix

The prefix to apply to generated selectors.

--skipInstall=true|false --skipInstall|--noSkipInstall

Skip installing dependency packages.

--skipPackageJson=true|false --skipPackageJson|--noSkipPackageJson

Do not add dependencies to package.json.

--skipTsConfig=true|false --skipTsConfig|--noSkipTsConfig

Do not update tsconfig.json for development experience.

module

ng generate module <name> [options]ng g module <name> [options]

参数

参数说明
name

The name of the module.

选项

选项说明
--flat=true|false --flat|--noFlat

Flag to indicate if a dir is created.

--module=module
-m=module

Allows specification of the declaring module.

--project=project

The name of the project.

--routing=true|false --routing|--noRouting

Generates a routing module.

--routingScope=Child|Root

The scope for the generated routing.

pipe

ng generate pipe <name> [options]ng g pipe <name> [options]

参数

参数说明
name

The name of the pipe.

选项

选项说明
--export=true|false --export|--noExport

Specifies if declaring module exports the pipe.

--flat=true|false --flat|--noFlat

Flag to indicate if a dir is created.

--lintFix=true|false --lintFix|--noLintFix

Specifies whether to apply lint fixes after generating the pipe.

--module=module
-m=module

Allows specification of the declaring module.

--project=project

The name of the project.

--skipImport=true|false --skipImport|--noSkipImport

Allows for skipping the module import.

--spec=true|false --spec|--noSpec

Specifies if a spec file is generated.

service

ng generate service <name> [options]ng g service <name> [options]

参数

参数说明
name

The name of the service.

选项

选项说明
--flat=true|false --flat|--noFlat

Flag to indicate if a dir is created.

--lintFix=true|false --lintFix|--noLintFix

Specifies whether to apply lint fixes after generating the pipe.

--project=project

The name of the project.

--spec=true|false --spec|--noSpec

Specifies if a spec file is generated.

serviceWorker

ng generate serviceWorker [options]ng g serviceWorker [options]

选项

选项说明
--configuration=configuration

The configuration to apply service worker to.

--project=project

The name of the project.

--target=target

The target to apply service worker to.

universal

ng generate universal [options]ng g universal [options]

选项

选项说明
--appDir=appDir

The name of the application directory.

--appId=appId

The appId to use withServerTransition.

--clientProject=clientProject

Name of related client app.

--main=main

The name of the main entry-point file.

--rootModuleClassName=rootModuleClassName

The name of the root module class.

--rootModuleFileName=rootModuleFileName

The name of the root module file

--skipInstall=true|false --skipInstall|--noSkipInstall

Skip installing dependency packages.

--test=test

The name of the test entry-point file.

--testTsconfigFileName=testTsconfigFileName

The name of the TypeScript configuration file for tests.

--tsconfigFileName=tsconfigFileName

The name of the TypeScript configuration file.