Introduction to Angular Angular is an open source web front-end framework developed by Google. It was born in 2009 and was created by Misko Hevery and others. It was later acquired by Google. It is an excellent front-end JS framework that has been used in many Google products. Angular is based on TypeScript and react. Compared with vue, Angular is more suitable for medium and large enterprise projects. Regarding Angular versions, Angular officially has unified the naming of Angular 1.x as Angular JS; Angular 2.x and above are collectively referred to as Angular; Currently the latest version of angular is angular9.x as of December 25, 2019. According to the official introduction, Angular will update a version every few months. The usage of all Angular versions after Angular2.x is the same. This tutorial is also applicable to Angular7.x, Angular8.x, Angular9.x and other future versions... Essential basics for learning Angular: html, css, js, es6, ts 1. Install the development environmentnpm install -g typescript npm install -g @angular/cli 2. Create a hello-world projectCreate a project ng new angular2-hello-world View the directory structure of the new project cd angular2-hello-world sudo apt install tree tree -F -L 1 . ├── angular.json ├── karma.conf.js ├── node_modules/ ├── package.json ├── package-lock.json ├── README.md ├── src/ ├── tsconfig.app.json ├── tsconfig.json └── tsconfig.spec.json 2 directories, 8 files View the structure in the src directory cd src tree -F Start the application and view the running results at http://localhost:4200 ng serve Create a hello-world component ng-generate component hello-world Define a new component in hello-world.component.ts //Import dependenciesimport { Component, OnInit } from '@angular/core'; //Declare the control's selector and related file URLs through annotations @Component({ selector: 'app-hello-world', templateUrl: './hello-world.component.html', styleUrls: ['./hello-world.component.css'] }) //Component data model export class HelloWorldComponent implements OnInit { constructor() { } ngOnInit(): void { } } Define the template in hello-world.component.html <p>mango, hello-world works!</p> In order to use the newly added components, we put Add the tag to app.component.html. <h1> <app-hello-world></app-hello-world> </h1> Execute ng serve to view the execution effect 3. Create a user-item directive to display usersGenerate instruction components mango@mango:~/angular2-hello-world$ ng generate component user-item CREATE src/app/user-item/user-item.component.css (0 bytes) CREATE src/app/user-item/user-item.component.html (24 bytes) CREATE src/app/user-item/user-item.component.spec.ts (641 bytes) CREATE src/app/user-item/user-item.component.ts (286 bytes) UPDATE src/app/app.module.ts (585 bytes) Declare and initialize a name field for the component import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-user-item', templateUrl: './user-item.component.html', styleUrls: ['./user-item.component.css'] }) export class UserItemComponent implements OnInit { name: string, constructor() { this.name = 'mango'; } ngOnInit(): void { } } Display the value of the variable name in the template <p> {{name}} welcome into Angular world. </p> Add app-user-item to app.component.html and view the browser execution results. 4. Create a user list user-list commandCreating a New Component mango@mango:~/angular2-hello-world$ ng generate component user-list CREATE src/app/user-list/user-list.component.css (0 bytes) CREATE src/app/user-list/user-list.component.html (24 bytes) CREATE src/app/user-list/user-list.component.spec.ts (641 bytes) CREATE src/app/user-list/user-list.component.ts (286 bytes) UPDATE src/app/app.module.ts (677 bytes) Declare and initialize the names array in the component import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-user-list', templateUrl: './user-list.component.html', styleUrls: ['./user-list.component.css'] }) export class UserListComponent implements OnInit { names: string[]; constructor() { this.names = ['mango', 'pear', 'grap', 'apple']; } ngOnInit(): void { } } Recursively traverse the names array in the component's template <ul> <li *ngFor="let name of names">Hello {{name}}</li> </ul> Add the component to app.component.html and check the browser execution results. 5. Combining user-item and user-listModify the name parameter of user-item to use external input import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'app-user-item', templateUrl: './user-item.component.html', styleUrls: ['./user-item.component.css'] }) export class UserItemComponent implements OnInit { @Input() name!: string; constructor() { } ngOnInit(): void { } } Modify the template of user-list <ul> <app-user-item *ngFor="let name of names" [name]="name"></app-user-item> </ul> Save and view browser execution. 6. Startup process analysisng will first look for the main entry point of the program in angular.json, which is src/main.ts { "outputPath": "dist/angular2-hello-world", "index": "src/index.html", "main": "src/main.ts", "polyfills": "src/polyfills.ts", "tsConfig": "tsconfig.app.json", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "src/styles.css" ], "scripts": [] } Check the main.ts file and find that the started Module is AppModule, located in app/app.module.ts import { enableProdMode } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; import { environment } from './environments/environment'; if (environment.production) { enableProdMode(); } platformBrowserDynamic().bootstrapModule(AppModule) .catch(err => console.error(err)); In app.module.ts, we can see that the components in this module, the dependent external modules, and the AppComponent started as the top-level component are declared through the NgModule annotation; import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { HelloWorldComponent } from './hello-world/hello-world.component'; import { UserItemComponent } from './user-item/user-item.component'; import { UserListComponent } from './user-list/user-list.component'; @NgModule({ declarations: [ AppComponent, HelloWorldComponent, UserItemComponent, UserListComponent ], imports: [ BrowserModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { } The above is the details of Angular environment construction and simple experience. For more information about Angular environment construction, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
>>: Analysis of implicit bug in concurrent replication of MySQL 5.7
1. Introduction to Data Integrity 1. Introduction...
1. Download MySQL Community Server 5.6.35 Downloa...
This article uses the gearman+mysql method to imp...
Summary: What method should be used for MySQL JDB...
Table of contents 1. React.FC<> 2. class xx...
Find two test machines: [root@docker1 centos_zabb...
There are two ways to deploy Angular projects wit...
This article example shares the specific code of ...
Use native JS to write a nine-square grid to achi...
0. Preparation: • Close iTunes • Kill the service...
1. Install Fcitx input framework Related dependen...
Table of contents Drop-down multiple-select box U...
This article example shares the specific code of ...
Preface The logical judgment statements we use in...
1. Enter the host machine of the docker container...