Home Web Front-end JS Tutorial How Angular CLI implements an Angular project

How Angular CLI implements an Angular project

Jun 06, 2018 pm 03:26 PM
angular

This time I will show you how Angular CLI implements an Angular project, and what are the precautions for how Angular CLI implements an Angular project. The following is a practical case, let's take a look.

Angular CLI is a command line interface tool that can create projects, add files, and perform a lot of development tasks, such as testing, packaging, and publishing. The quick start here is based on this command.

Before starting the project, you need to install node and npm first, and then execute npm install -g @angular/cli to install Angular CLI.

1: Use the command line to create a new project

1

2

3

4

ng new newApp --skip-install

cd newApp

cnpm install

ng serve --open

Copy after login

Executing the above command will automatically create a new Angualr project and start the project.

Among them, --skip-install means that the node package will not be installed first. It will be much faster if we then use cnpm install to install it.

2: Directory structure

Now let’s take a look at what the ng command helps us generate, which is the directory structure of the project, which is First, have a general understanding of what it does. You don’t have to know everything, but first remember the following ones that I feel are important:

1.src: where the application code is stored;

2 .src/app: The main place where your code is stored. This may not be appropriate, but when you develop, you spend most of your time modifying the code here;

3.src/assets: pictures, etc. Where it is stored, it will be copied to the release package when building;

4.src/main.js: You will basically not modify it, it is the main entry of the program;

5.src /styles.css: Specially used styles are written in the corresponding places. As will be mentioned later, public styles will be written here;

6.karma.conf.js: Unit test configuration for Karma. It is used when running ng test.

3: Custom component

1

2

3

4

5

6

7

8

9

10

11

12

13

14

import { Component } from '@angular/core';

@Component({

  selector: 'my-comp',

  template: '<ul><li *ngFor=&#39;let row of dataList&#39;>ID:{{row.id}} INFO:{{row.info}}</li></ul>',  

  styles: [`ul{background-color: antiquewhite;padding-left: 10px;list-style: none;}`]

   

})

export class MyComponent {

  dataList = [

    { id: 1, info: "Angular" },

    { id: 2, info: "React" },

    { id: 3, info: "Vue" }

  ];  

}

Copy after login

A very simple component has been defined above, but before using it, you still need Defined in the module, this time it is registered in src/app/app.module.ts:

1

2

3

4

5

6

7

8

import { NgModule } from '@angular/core';

import { MyComponent } from './my.component';

@NgModule({

  declarations: [

    MyComponent

  ]  

})

......

Copy after login

Now that it is registered, you can use it. The method of using the above example is very simple, just do it yourself A label my-comp is defined, which is used exactly the same as ordinary p.

It should be noted that for the convenience of viewing, I have removed irrelevant code in the registration example. Fortunately, the actual situation includes registration of startup, other components, services, etc. You can look at the command line For other instructions automatically generated, here we mainly explain more important things, the same below.

Similar to AngularJS, Angular's selector has other features besides the custom tags above:

1.selector: 'element-name'//Custom tag selector;

2.selector: '.class'//Style selector;

3.selector: '[attribute]'//Attribute selector;

4.selector: '[ attribute=value]'//Attribute value selector;

5.selector: ':not(sub_selector)'//Invert selector;

6.selector: 'selector1, selector2 '//Multiple selectors.

Four: Custom service

Like components, let’s define a service first.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

import { Injectable } from '@angular/core';

export class DataFormat { 

  id: number; 

  info: string; 

}

@Injectable()

export class MyServ {

  getData(): DataFormat[] {

    return [

      { id: 1, info: "Angular" }, 

      { id: 2, info: "React" }, 

      { id: 3, info: "Vue" }

    ];

  }  

}

Copy after login

Next, let’s register it. The registration of services and components is a bit different. Let’s register it on the main component first. The default is to register it in the arc/app/app.component.ts file:

The use of the

1

2

3

4

5

import { Component } from '@angular/core';

import { MyServ } from './my.service';

@Component({

  providers: [MyServ]  

})

Copy after login

service is also very simple. Let’s use the constructor to demonstrate it here:

1

2

3

4

5

6

7

8

import { MyServ } from './my.service';

......

export class MyComponent {

  dataList: any[];  

  constructor(private demoService: MyServ) {

     this.dataList = this.demoService.getData();

    }  

}

Copy after login

Do you remember the code of the custom component? We demonstrated the usage of the service in it, and only the modified part of the code is given above.

5: The use of routing

We give a simple usage of routing here. The specific details are similar to the above and will be discussed separately. To discuss, the purpose of this article is to get started quickly.

For the convenience of demonstration, we have defined two components by default: MyComponent and My2Component.

First of all, you need to make sure that is defined in the head tag of the index.html page or the script that dynamically generates this element.

We first register the route in src/app/app.module.ts:

1

2

3

4

5

6

7

8

9

10

11

12

13

......

import { RouterModule } from '@angular/router';

@NgModule({

  declarations: [MyComponent,My2Component],  

  imports: [  

    RouterModule.forRoot([    

      {path: 'my',component: MyComponent},      

      {path: 'my2',component: My2Component}      

    ])

  ]

  ......

})

......

Copy after login

It is very simple to use:

1

2

3

<a routerLink="/my">toMycomp</a>

<a routerLink="/my2">toMy2comp</a>

<router-outlet></router-outlet>

Copy after login

点击toMycomp或者toMy2comp就会跳转对应的路由设置的组件了。

六:HTTP

由于@angular/http库中的HttpModule保存着http相关的服务,需要先引入进来(这里是在src/app/app.module.ts中引入):

1

2

3

4

5

import { HttpModule }  from '@angular/http';

@NgModule({

  imports: [HttpModule]  

})

......

Copy after login

现在,http就是一个服务,下面简单演示一种用法:

1

2

3

4

5

6

7

8

9

......

import { Http } from '@angular/http';

......

  constructor(private http: Http) {  

    http.get('assets/XXX.json').forEach(function (data) {    

      console.log(data['_body']);      

    });    

  }  

......

Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

移动端怎样做出上拉下滑刷新加载

Vue做出弹窗功能(附代码)

The above is the detailed content of How Angular CLI implements an Angular project. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Let's talk about metadata and decorators in Angular Let's talk about metadata and decorators in Angular Feb 28, 2022 am 11:10 AM

This article continues the learning of Angular, takes you to understand the metadata and decorators in Angular, and briefly understands their usage. I hope it will be helpful to everyone!

How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

Detailed explanation of angular learning state manager NgRx Detailed explanation of angular learning state manager NgRx May 25, 2022 am 11:01 AM

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

An article exploring server-side rendering (SSR) in Angular An article exploring server-side rendering (SSR) in Angular Dec 27, 2022 pm 07:24 PM

Do you know Angular Universal? It can help the website provide better SEO support!

How to use PHP and Angular for front-end development How to use PHP and Angular for front-end development May 11, 2023 pm 04:04 PM

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

Angular + NG-ZORRO quickly develop a backend system Angular + NG-ZORRO quickly develop a backend system Apr 21, 2022 am 10:45 AM

This article will share with you an Angular practical experience and learn how to quickly develop a backend system using angualr combined with ng-zorro. I hope it will be helpful to everyone!

A brief analysis of how to use monaco-editor in angular A brief analysis of how to use monaco-editor in angular Oct 17, 2022 pm 08:04 PM

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

A brief analysis of independent components in Angular and see how to use them A brief analysis of independent components in Angular and see how to use them Jun 23, 2022 pm 03:49 PM

This article will take you through the independent components in Angular, how to create an independent component in Angular, and how to import existing modules into the independent component. I hope it will be helpful to you!

See all articles