Angular Environment Setup and basicconcepts

LearningAngular basicconcepts, includingEnvironment Setup, projectstructure, component, 模板 and moduleetc.Basicsknowledge

Angular Introduction

Angular is a 由GoogleDevelopment open-source before 端framework, 用于构建动态Webapplication程序. 它providing了完整 solution, including模板system, 依赖注入, routing, 表单processingetc.corefunctions. AngularadoptsTypeScriptDevelopment, 具 has 强 big class型check and 面向objectfeatures, 适合构建 big 型, complex 企业级application.

提示

Angular and AngularJS is 两个不同 framework. AngularJS is Angular before 身, adoptsJavaScriptDevelopment, 而Angular (通常称 for Angular 2+) is a 完全重写 framework, adoptsTypeScriptDevelopment.

Angular corefeatures

  • component化architecture: 将application分解 for reusable component
  • module化design: usingmodule组织code, improvingcode 可maintenance性 and 可test性
  • 依赖注入: 简化component间 依赖management
  • response式programming: usingRxJSprocessingasynchronousoperation and event流
  • 模板system: usingHTML模板结合Angular指令构建user界面
  • 表单processing: providing模板驱动表单 and response式表单两种表单processing方式
  • routingmanagement: support单页application routing导航
  • testsupport: providing完整 testtool链

Environment Setup

要开始usingAngular, 需要搭建Developmentenvironment. 以 under is 搭建AngularDevelopmentenvironment 步骤:

1. installation Node.js

Angular需要Node.jsenvironment. 首先, 你需要installationNode.js. 你可以 from Node.js官网 under 载并installation适合你operationsystem Node.jsversion.

installationcompletion after , 你可以using以 under commandscheckNode.js and npm (Node.jspackagemanagement器) version:

node -v
npm -v

2. installation Angular CLI

Angular CLI is Angular commands行tool, 用于creation and managementAngularproject. using以 under commandsinstallationAngular CLI:

npm install -g @angular/cli

installationcompletion after , 你可以using以 under commandscheckAngular CLI version:

ng version

3. creation Angular project

usingAngular CLIcreation一个 new Angularproject:

ng new my-angular-app

in creationproject时, Angular CLI会询问你 is 否要添加routing and using哪种样式表格式. 你可以根据自己 需要选择.

4. 启动Developmentserver

进入projectTable of Contents并启动Developmentserver:

cd my-angular-app
ng serve

Developmentserver启动 after , 你可以 in 浏览器in访问 http://localhost:4200 查看你 Angularapplication.

Angular projectstructure

creationAngularproject after , 你会看 to 以 under 主要file and Table of Contentsstructure:

my-angular-app/
├── e2e/                     # 端 to 端test
├── node_modules/             # 依赖package
├── src/                      # sourcescode
│   ├── app/                  # applicationcode
│   │   ├── app.component.css  # 根component样式
│   │   ├── app.component.html # 根component模板
│   │   ├── app.component.spec.ts # 根componenttest
│   │   ├── app.component.ts  # 根componentcode
│   │   ├── app.module.ts      # 根module
│   │   └── app-routing.module.ts # routingmodule (such as果添加了routing) 
│   ├── assets/               # 静态resource
│   ├── environments/         # environmentconfiguration
│   ├── favicon.ico           # 网站graph标
│   ├── index.html            # 入口HTMLfile
│   ├── main.ts               # 入口TypeScriptfile
│   ├── polyfills.ts          # compatibilityprocessing
│   ├── styles.css            # 全局样式
│   └── test.ts               # test入口file
├── .angular-cli.json         # Angular CLIconfiguration
├── .editorconfig             # 编辑器configuration
├── .gitignore                # Gitignorefile
├── karma.conf.js             # Karmatestconfiguration
├── package.json              # projectconfiguration and 依赖
├── protractor.conf.js        # Protractortestconfiguration
├── README.md                 # project说明
├── tsconfig.json             # TypeScriptconfiguration
└── tslint.json               # TSLintconfiguration

corefile说明

  • src/main.ts: application 入口file, 负责引导Angularapplication
  • src/index.html: application HTML模板, Angular会将编译 after component插入 to 这个filein
  • src/app/app.module.ts: application 根module, 负责声明 and configurationapplication component, serviceetc.
  • src/app/app.component.ts: application 根component, is application 顶级component
  • src/app/app.component.html: 根component 模板file
  • src/app/app.component.css: 根component 样式file

Angular basicconcepts

1. component (Component)

component is Angularapplication basic构建块, 每个componentpackage含模板, 样式 and 逻辑code. component用于构建user界面 不同部分.

一个典型 Angularcomponentincluding:

  • 模板 (Template) : usingHTML定义component 视graphstructure
  • 样式 (Styles) : 定义component out 观
  • 控制器 (Controller) : usingTypeScriptclass定义component behavior and data

以 under is a simple Angularcomponentexample:

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

@Component({
  selector: 'app-hello',
  template: `

Hello, {{ name }}!

`, styles: [`h1 { color: blue; }`] }) export class HelloComponent { name = 'Angular'; }

2. module (Module)

module is 组织Angularapplication containers, 用于将相关 component, service, 指令etc.组织 in 一起. 每个Angularapplication至 few has 一个根module (AppModule) .

以 under is a simple Angularmoduleexample:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';

@NgModule({
  declarations: [
    AppComponent,
    HelloComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

3. 指令 (Directive)

指令 is Angular模板in 特殊标记, 用于scaleHTML functions. Angular has 三种class型 指令:

  • component指令: 带 has 模板 指令, is Angularapplication basic构建块
  • property指令: 用于modifyDOM元素 out 观 or behavior, such asngClass, ngStyleetc.
  • structure指令: 用于modifyDOMtree structure, such asngIf, ngForetc.

4. service (Service)

service is 用于encapsulation可复用逻辑 class, such asdata获取, 业务逻辑etc.. service可以被 many 个component共享, improvingcode reusability and 可maintenance性.

以 under is a simple Angularserviceexample:

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

@Injectable({
  providedIn: 'root'
})
export class DataService {
  getMessage() {
    return 'Hello from DataService!';
  }
}

5. 依赖注入 (Dependency Injection)

依赖注入 is Angular corefeatures之一, 用于managementcomponent and service之间 依赖relationships. through依赖注入, Angular会自动creation and providingcomponent所需 serviceinstance.

以 under is in componentinusing依赖注入 example:

import { Component } from '@angular/core';
import { DataService } from './data.service';

@Component({
  selector: 'app-example',
  template: `

{{ message }}

` }) export class ExampleComponent { message: string; constructor(private dataService: DataService) { this.message = this.dataService.getMessage(); } }

第一个 Angular application

现 in , 让我们creation一个 simple Angularapplication, 来verification我们 environment is 否搭建成功.

步骤 1: creationproject

usingAngular CLIcreation一个 new Angularproject:

ng new my-first-angular-app

步骤 2: 进入projectTable of Contents

cd my-first-angular-app

步骤 3: 启动Developmentserver

ng serve --open

这个commands会启动Developmentserver, 并自动 in 浏览器in打开application. 你应该会看 to Angular 欢迎页面.

步骤 4: modifyapplication

让我们modify根component, creation一个 simple Hello Worldapplication. 打开src/app/app.component.htmlfile, 将其 in 容replace for :

Hello, Angular!

这 is 我 第一个Angularapplication.

保存file after , 浏览器会自动刷 new , 你应该会看 to modify after 页面.

练习 1: creationAngularapplication

  1. installationNode.js and Angular CLI
  2. usingAngular CLIcreation一个 new Angularproject
  3. 启动Developmentserver并查看application
  4. modify根component, 添加一个标题 and 一段describes
  5. 保存file, 查看浏览器in 变化

练习 2: creation自定义component

  1. usingAngular CLIcreation一个 new component: ng generate component hello
  2. modifyhellocomponent 模板, 添加一个问候语
  3. in 根componentinusing这个 new component
  4. 保存file, 查看浏览器in 变化