ng generate

Generates and/or modifies files based on a schematic.

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

参数

参数说明
<schematic>

The schematic or collection:schematic to generate.

该选项可以接受下列子命令之一:

选项

选项说明
--defaults=true|false

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

默认值: false

--dryRun=true|false

When true, runs through and reports activity without writing out results.

默认值: false

别名: -d

--force=true|false

When true, forces overwriting of existing files.

默认值: false

别名: -f

--help=
true|false|json|JSON

Shows a help message for this command in the console.

默认值: false

--interactive=true|false

When false, disables interactive input prompts.

默认值: false

Schematic 命令

appShell

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

Generates an app shell for running a server-side version of an app.

选项

选项说明
--appDir=appDir

The name of the application directory.

默认值: app

--appId=appId

The app ID to use in withServerTransition().

默认值: serverApp

--clientProject=clientProject

The name of the related client app.

--index=index

The name of the index file

默认值: index.html

--main=main

The name of the main entry-point file.

默认值: main.server.ts

--name=name

The HTML selector of the Universal app

--outDir=outDir

The output directory for build results.

默认值: dist-server

--root=root

The root directory of the app.

默认值: src

--rootModuleClassName=
rootModuleClassName

The name of the root module class.

默认值: AppServerModule

--rootModuleFileName=
rootModuleFileName

The name of the root module file

默认值: app.server.module.ts

--route=route

Route path used to produce the app shell.

默认值: shell

--sourceDir=sourceDir

The path of the source directory.

默认值: src

别名: -D

--test=test

The name of the test entry-point file.

--testTsconfigFileName=
testTsconfigFileName

The name of the TypeScript configuration file for tests.

默认值: tsconfig.spec

--tsconfigFileName=
tsconfigFileName

The name of the TypeScript configuration file.

默认值: tsconfig.server

--universalProject=
universalProject

The name of related Universal app.

application

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

Generates a new basic app definition in the "projects" subfolder of the workspace.

参数

参数说明
<name>

The name of the new app.

选项

选项说明
--experimentalIvy=true|false

EXPERIMENTAL: True to create a new app that uses the Ivy rendering engine.

默认值: false

--inlineStyle=true|false

When true, includes 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.

默认值: false

别名: -s

--inlineTemplate=true|false

When true, includes 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.

默认值: false

别名: -t

--lintFix=true|false

When true, applies lint fixes after generating the application.

默认值: false

--minimal=true|false

When true, creates a bare-bones project without any testing frameworks.

默认值: false

--prefix=prefix

A prefix to apply to generated selectors.

默认值: app

别名: -p

--routing=true|false

When true, creates a routing NgModule.

默认值: false

--skipInstall=true|false

Skip installing dependency packages.

默认值: false

--skipPackageJson=true|false

When true, does not add dependencies to the "package.json" file.

默认值: false

--skipTests=true|false

When true, does not create "spec.ts" test files for the app.

默认值: false

别名: -S

--style=style

The file extension to use for style files.

默认值: css

--viewEncapsulation=
Emulated|Native|None|ShadowDom

The view encapsulation strategy to use in the new app.

class

ng generate class <name> [options]
      
      ng generate class <name> [options]
    
ng g 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.

选项

选项说明
--lintFix=true|false

When true, applies lint fixes after generating the class.

默认值: false

--project=project

The name of the project.

--skipTests=true|false

When true, does not create "spec.ts" test files for the new class.

默认值: false

--spec=true|false

When true (the default), generates a "spec.ts" test file for the new class.

默认值: true

--type=type

Adds a developer-defined type to the filename, in the format "name.type.ts".

默认值:

component

ng generate component <name> [options]
      
      ng generate component <name> [options]
    
ng g 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.

选项

选项说明
--changeDetection=Default|OnPush

The change detection strategy to use in the new component.

默认值: Default

别名: -c

--entryComponent=true|false

When true, the new component is the entry component of the declaring NgModule.

默认值: false

--export=true|false

When true, the declaring NgModule exports this component.

默认值: false

--flat=true|false

When true, creates the new files at the top level of the current project.

默认值: false

--inlineStyle=true|false

When true, includes 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.

默认值: false

别名: -s

--inlineTemplate=true|false

When true, includes template inline in the component.ts file. By default, an external template file is created and referenced in the component.ts file.

默认值: false

别名: -t

--lintFix=true|false

When true, applies lint fixes after generating the component.

默认值: false

--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

The HTML selector to use for this component.

--skipImport=true|false

When true, does not import this component into the owning NgModule.

默认值: false

--skipTests=true|false

When true, does not create "spec.ts" test files for the new component.

默认值: false

--spec=true|false

When true (the default), generates a "spec.ts" test file for the new component.

默认值: true

--style=style

The file extension to use for style files.

默认值: css

--styleext=styleext

The file extension to use for style files.

默认值: css

--viewEncapsulation=
Emulated|Native|None|ShadowDom

The view encapsulation strategy to use in the new component.

别名: -v

directive

ng generate directive <name> [options]
      
      ng generate directive <name> [options]
    
ng g 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

When true, the declaring NgModule exports this directive.

默认值: false

--flat=true|false

When true (the default), creates the new files at the top level of the current project.

默认值: true

--lintFix=true|false

When true, applies lint fixes after generating the directive.

默认值: false

--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

The HTML selector to use for this directive.

--skipImport=true|false

When true, does not import this directive into the owning NgModule.

默认值: false

--skipTests=true|false

When true, does not create "spec.ts" test files for the new class.

默认值: false

--spec=true|false

When true (the default), generates a "spec.ts" test file for the new directive.

默认值: true

enum

ng generate enum <name> [options]
      
      ng generate enum <name> [options]
    
ng g 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.

选项

选项说明
--lintFix=true|false

When true, applies lint fixes after generating the enum.

默认值: false

--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 generate guard <name> [options]
    
ng g 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

When true (the default), creates the new files at the top level of the current project.

默认值: true

--lintFix=true|false

When true, applies lint fixes after generating the guard.

默认值: false

--project=project

The name of the project.

--skipTests=true|false

When true, does not create "spec.ts" test files for the new guard.

默认值: false

--spec=true|false

When true (the default), generates a "spec.ts" test file for the new guard.

默认值: true

interface

ng generate interface <name> <type> [options]
      
      ng generate interface <name> <type> [options]
    
ng g 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>

Adds a developer-defined type to the filename, in the format "name.type.ts".

选项

选项说明
--lintFix=true|false

When true, applies lint fixes after generating the interface.

默认值: false

--prefix=prefix

A prefix to apply to generated selectors.

默认值:

--project=project

The name of the project.

library

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

Creates a new generic library project in the current workspace.

参数

参数说明
<name>

The name of the library.

选项

选项说明
--entryFile=entryFile

The path at which to create the library's public API file, relative to the workspace root.

默认值: public_api

--lintFix=true|false

When true, applies lint fixes after generating the library.

默认值: false

--prefix=prefix

A prefix to apply to generated selectors.

默认值: lib

别名: -p

--skipInstall=true|false

When true, does not install dependency packages.

默认值: false

--skipPackageJson=true|false

When true, does not add dependencies to the "package.json" file.

默认值: false

--skipTsConfig=true|false

When true, does 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.

默认值: false

module

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

Creates a new generic NgModule definition in the given or default project.

参数

参数说明
<name>

The name of the NgModule.

选项

选项说明
--flat=true|false

When true, creates the new files at the top level of the current project root.

默认值: false

--lintFix=true|false

When true, applies lint fixes after generating the module.

默认值: false

--module=module

The declaring NgModule.

别名: -m

--project=project

The name of the project.

--routing=true|false

When true, creates a routing module.

默认值: false

--routingScope=Child|Root

The scope for the new routing module.

默认值: Child

pipe

ng generate pipe <name> [options]
      
      ng generate pipe <name> [options]
    
ng g 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

When true, the declaring NgModule exports this pipe.

默认值: false

--flat=true|false

When true (the default) creates files at the top level of the project.

默认值: true

--lintFix=true|false

When true, applies lint fixes after generating the pipe.

默认值: false

--module=module

The declaring NgModule.

别名: -m

--project=project

The name of the project.

--skipImport=true|false

When true, does not import this pipe into the owning NgModule.

默认值: false

--skipTests=true|false

When true, does not create "spec.ts" test files for the new pipe.

默认值: false

--spec=true|false

When true (the default), generates a "spec.ts" test file for the new pipe.

默认值: true

service

ng generate service <name> [options]
      
      ng generate service <name> [options]
    
ng g 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

When true (the default), creates files at the top level of the project.

默认值: true

--lintFix=true|false

When true, applies lint fixes after generating the pipe.

默认值: false

--project=project

The name of the project.

--skipTests=true|false

When true, does not create "spec.ts" test files for the new service.

默认值: false

--spec=true|false

When true (the default), generates a "spec.ts" test file for the new service.

默认值: true

serviceWorker

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

Pass this schematic to the "run" command to create a service worker

选项

选项说明
--configuration=configuration

The configuration to apply service worker to.

默认值: production

--project=project

The name of the project.

--target=target

The target to apply service worker to.

默认值: build

universal

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

Pass this schematic to the "run" command to set up server-side rendering for an app.

选项

选项说明
--appDir=appDir

The name of the application folder.

默认值: app

--appId=appId

The app identifier to use for transition.

默认值: serverApp

--clientProject=clientProject

The name of the related client app. Required in place of "project".

--main=main

The name of the main entry-point file.

默认值: main.server.ts

--rootModuleClassName=
rootModuleClassName

The name of the root NgModule class.

默认值: AppServerModule

--rootModuleFileName=
rootModuleFileName

The name of the root NgModule file.

默认值: app.server.module.ts

--skipInstall=true|false

When true, does not install packages for dependencies.

默认值: false

--test=test

The name of the test entry-point file.

--testTsconfigFileName=
testTsconfigFileName

The name of the TypeScript configuration file for tests.

默认值: tsconfig.spec

--tsconfigFileName=
tsconfigFileName

The name of the TypeScript configuration file.

默认值: tsconfig.server