Home >Web Front-end >JS Tutorial >Detailed explanation of using Angular CLI to generate code from blueprints

Detailed explanation of using Angular CLI to generate code from blueprints

亚连
亚连Original
2018-05-29 11:05:381563browse

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 bcd3001c833876023595adb6a89868d3 command.

The format of this command is ng generate f05eccb4ed5fda202e425eeb1a8afbcd 9a638e57bd3075e0a0acbe78463659d6.

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