← 返回命令列表

Linux command

ng-generate 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Generate component

ng generate component [name]

Generate service

ng generate service [name]

Generate module

ng generate module [name]

Generate with shorthand

ng g c [name]

Generate in specific path

ng generate component [path/name]

Dry run

ng generate component [name] --dry-run

说明

ng generate creates Angular artifacts using schematics. It scaffolds components, services, modules, directives, pipes, guards, interceptors, and more, wiring them into the project's NgModule (or generating them as standalone in newer Angular versions) and adding matching .spec.ts files. The shorthand ng g is equivalent. Schematics live in @schematics/angular and other npm packages; libraries shipped via ng add can register their own (for example, @angular/material adds material:navigation). The available list can be inspected with ng generate --help.

参数

--dry-run, -d
Show what would be generated without writing files.
--force
Overwrite existing files.
--skip-tests
Do not generate .spec.ts test files.
--skip-import
Do not import the generated piece into its NgModule.
--standalone
Generate a standalone component/directive/pipe (Angular 14+).
--inline-template, -t
Place HTML inline in the component decorator.
--inline-style, -s
Place CSS inline in the component decorator.
--style _ext_
Stylesheet extension: css, scss, sass, less, none.
--prefix _prefix_
Override the project's selector prefix.
--export
Export the generated declaration from its NgModule.
--project _name_
Target a specific project in the workspace.

FAQ

What is the ng-generate command used for?

ng generate creates Angular artifacts using schematics. It scaffolds components, services, modules, directives, pipes, guards, interceptors, and more, wiring them into the project's NgModule (or generating them as standalone in newer Angular versions) and adding matching .spec.ts files. The shorthand ng g is equivalent. Schematics live in @schematics/angular and other npm packages; libraries shipped via ng add can register their own (for example, @angular/material adds material:navigation). The available list can be inspected with ng generate --help.

How do I run a basic ng-generate example?

Run `ng generate component [name]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does --dry-run, -d do in ng-generate?

Show what would be generated without writing files.