search
HomeWeb Front-endJS TutorialDetailed explanation of using Angular CLI to generate code from blueprints

This article mainly introduces the detailed explanation of using Angular CLI to generate code from a blueprint. Now I share it with you and give it as a reference.

The first article is: "Using angular cli to generate angular5 projects": http://www.jb51.net/article/136621.htm

This article mainly talks about generating Components , Directive, Service, class, interface, enum, etc.

ng generate command.

The format of this command is ng generate .

You can also use the --dry-run parameter to list the files to be generated instead of actually generating them.

For example:

ng generate component person, a person.component.ts will be generated.

ng generate service sales-data will generate a sales-data.service.ts.

ng generate class user-model will generate a user-model.ts which contains the UserModel class.

#Components.

ng The command to generate xxx xxx is still a bit long, here angular cli has built-in aliases for commands, for example:

ng generate component person In this command, the generate in

can be replaced by the letter g, and the component in

can be replaced by the letter c Replace.

So these two commands are equal:

ng generate component person
ng g c person

You can go here to view component-related commands and aliases:

https://github.com/angular/angular-cli/wiki/generate-component

Possibly commonly used command parameters are:

  1. ##--flat Indicates whether there is no need to create a folder

  2. --inline-template (-it) Whether the template should be placed in the ts file

  3. -- inline-style (-is) Whether the style should be placed in the ts file.

  4. --spec Whether it is necessary to create a spec file (test file)

  5. --view-encapsulation (-ve) View Encapsulation strategy (simply understood as the scope strategy of style files).

  6. --change-detection (-cd) Change detection strategy .

  7. --prefix Set the prefix of this component

  8. --dry-run (-d), print out the generated file list , instead of directly generating.

Look at the following two pairs of commands that have the same effect. It is more convenient to use aliases:

ng generate component person
ng generate component person --inline-template --inline-style

ng g c person
ng g c person -it -is

Let’s try these commands:

Create the project: ng new my-app Wait until npm install is completed before proceeding.

After establishing the project, enter the directory and execute the command:

ng g c person -d

This command will generate the above 4 files and update app.module.ts.

Remove the -d parameter below and generate the file:

You can see that the file is generated in the project. And the app is updated. module.ts, declare the component in it.

Try to generate another component again, using some parameters View Encapsulation and Change Detection Strategy:

ng g c student -ve Emulated -cd OnPush

You can see that the parameters have taken effect.

Through the source code management page, you can see what updates these two commands have made to app.module:

Declare the two generated components respectively.

Then I commit...

Directive.

ng g d filter-box -d

This is the file report, the following is the real generation:

ng g d filter-box

Through the source code management of vscode, you can see the changes:

directive generated two files, which are the same as the component and were also declared in app.module.

Look at the directory structure:

The structure of the generated directive has no directory, which is flat.

If you don’t want to generate a flat style, If you want it to have its own folder, use the --flat parameter:

ng g d filter-box2 --flat false

##Now you have your own folder.

Commit it.

Service.

ng g s order-data -d

可以看到 这个命令会生成flat结构的service.

然后把-d去掉, 真实生成文件:

ng g s order-data

可以从源码管理看到, 只生成了两个文件, 并没有在app.module里面注册:

当然可以在这里写代码把刚才生成的service注册进去.

但是正确的做法是使用 -m 参数来指定需要注册的module:

ng g s order-data2 -m app

这次生成的order-data2 service就会在app.module.ts里面进行注册了.

然后再commit一下.

Model/Interface/Enum/Pipe.

model:

ng g cl models/user

这个命令会创建models文件夹, 然后在里面创建user这个model:

interface:

ng g i models/animal

enum:

ng g e models/gender

commit一下.

Pipe.

ng g p camel-case

除了生成两个文件之外, 这个命令默认也会更新app.module.

Module.

ng g m login

可以看到module默认是自带文件夹的.

然后我试试添加一个component, 目的是要在login module进行声明:

可以看到我要创建的welcome component默认是在app.module里面进行声明的, 这时候如果想要在login module进行声明, 就要使用 -m 参数:

去掉-d执行生成命令后:

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

使用Angular CLI生成路由的方法

使用Angular CLI进行Build(构建)和Serve详解

使用Angular CLI进行单元测试和E2E测试的方法

The above is the detailed content of Detailed explanation of using Angular CLI to generate code from blueprints. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor