SlideShare uma empresa Scribd logo
1 de 28
使用 TypeScript 駕馭
Web 世界的脫韁野馬
多奇數位創意有限公司
技術總監 黃保翕 ( Will 保哥 )
部落格:http://blog.miniasp.com/
以 Angular 2 開發框架為例
ANGULAR 2 簡介
Angular 2 Introduction
與 Angular 1.x 的比較
• 三大特色
– 效能改進 (Performance)
• 偵測變更:比 ng1 快 10 倍
• 渲染速度:比 ng1 快 5 倍 (Render & Re-render)
• 範本編譯:支援 Template 預先編譯機制
• 更小的 Library Size 與延遲載入機制
• 支援伺服器渲染機制 (Node.js & ASP.NET )
– 高生產力 (Productivity)
• 開發應用程式能夠用更簡潔的語法讓團隊更加容易上手跟維護
• 更強大的開發工具 Augury
• 移除超過 40+ 個 directives
– 多樣平台 (Versatility)
• 支援 Browser, Node.js, NativeScript, and more … 3
從框架轉向平台
4
Angular 2 的開發語言
TypeScript
ES 2016
ES 2015
ES5
5
Angular 2 的開發語言
• ES5
– 傳統 JavaScript 程式語言 ( IE9+ )
• ES 2015
– 此版本為 ES5 的「超集合」
– 具有新穎的 JavaScript 語言特性 ( let, const, for-of, … )
– 可透過 Babel 轉譯器將瀏覽器不支援的語法轉為 ES5 版本
• TypeScript
– 此版本為 ES 2015 的「超集合」
– 具有強型別特性、內建 ES5 轉譯器 (Transpiler)、更好的工具支
援
• Dart
– 非 JavaScript 家族的程式語言
– 具有強型別特性 6
Angular 2 的開發工具
• Visual Studio Code
• Visual Studio 2015
• Atom
• Sublime Text
• Plunker
7
建立第一個 ANGULAR 2 應用程式
Build your first Angular 2 Application
8
Angular 2 應用程式的組成
• App Component元件
• Child Component元件
• Services Component元件
• Pipe Component元件
Angular 2 頁面的組成
應用程式元件 + 樣板
( AppComponent + Templates )
頁首元件 + 樣板
( HeaderComponent )
子選單
元件 + 樣板
(SideComponent)
10
主要內容元件 + 樣板
(MainComponent)
Angular 2 結構剖析
11
Angular 2 結構剖析
• Module 應用程式被切分成許多「模組」
• Component 每個模組下有許多「元件」
• Template 每個元件都可能有自己的「樣板」
• Metadata 每個元件都可以標示「中繼資料」
• Data Binding 樣板與元件屬性、方法可以進行綁定
• Directive 將 DOM 轉換為多功能的「宣告命令」
• Service 由「服務」集中管理資料與運算邏輯
• Dependency Injection 由「相依注入」機制管理物件生命週期
12
Angular 2 元件的組成
範本
( Template )
• HTML 版面配置
• HTML 部分片段
• 資料繫結 (Bindings)
• 畫面命令 (Directives)
類別
( Class )
• 建構式 (Constructor)
• 屬性 (Properties)
• 方法 (Methods)
中繼資料
( Metadata )
• 裝飾器 (Decorator)
• 針對類別
• 針對屬性
• 針對方法
• 針對參數
13
手動建立基礎開發環境
1. 建立應用程式資料夾
2. 建立 tsconfig.json
3. 建立 package.json
4. 建立 typings.json
5. 建立 libraries & typings
6. 建立 index.html
7. 建立 main.ts (bootstrapper)
8. 設定 .gitignore 與建立版控
9. 設定 Visual Studio Code 開發工具 ( .vscode )
14
使用 Angular CLI 建立專案範本
• npm install -g angular-cli
• ng new PROJECT_NAME
• cd PROJECT_NAME
• ng serve
– http://localhost:4200/
• ng generate component my-new-component
縮寫語法:
ng g component my-new-component 15
複製現有的 Angular 2 專案範本
• 現有的專案範本
– miniasp/angular-2-boilerplate
– miniasp/angular-2-boilerplate-webpack
– AngularClass/angular2-webpack-starter
– angular/angular2-seed
– DanWahlin/Angular2-JumpStart
– johnpapa/angular2-tour-of-heroes
16
認識 ES 2015 / TypeScript 模組化技術
• 每個檔案都是一個「模組」( module )
• 每個模組內都可以「匯出」( export ) 公開的物件
• 匯出
export class Product {
pageTitle = "Hello World";
}
• 匯入
import { Product } from './product';
import { Product as prod } from './product';
import * as product from './product';
import './product'; 17
建立第一個元件 (根元件)
• 建立根元件 app/app.component.ts
export class AppComponent {
pageTitle: string = 'Hello World';
}
• 匯入 Angular 2 啟動器 app/main.ts
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
bootstrap(AppComponent);
18
認識 Angular 2 元件的命名規則
// 命名規則: PascalCase
export class AppComponent {
// 命名規則: camelCase
pageTitle : string = "Hello World";
// 命名規則: 動詞 + 名詞 with camelCase
printTitle() {
console.log(this.pageTitle);
}
}
19
認識 ES7 / TypeScript 的 Decorator
• 可以套用在
– 類別
– 屬性
– 方法
– 方法中的參數
• 永遠以 @ 開頭
• 不用分號結尾
• 用法跟 C# 的 Attributes 很像!! 20
修改首頁 HTML 內容
• 修改 index.html
<my-app>
Loading App...
</my-app>
21
建立第一個元件的 HTML 範本
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<div>
<div class='container'><h1>{{pageTitle}}</h1></div>
</div>
`
})
export class AppComponent {
pageTitle: string = 'Hello World';
}
22
建立子元件
• 建立模組檔案 ( *.ts )
star.component.ts
• 建立元件類別 (Class) 與 中繼資料 (Decorator)
@Component({ selector: 'posts', template: `xxxxx` })
export class StarComponent {
}
• 設定元件範本 (Template)
star.component.html
• 套用結構式命令 (Structure Directives)
*ngIf='products && products.length'
*ngFor='let product of products' 23
載入子元件
• 修改上層元件的 directives
@Component({
templateUrl: 'app/products/product-list.component.html',
directives: [StarComponent]
})
• app/products/product-list.component.html
<ai-star [rating]='product.starRating'
(ratingClicked)='onRatingClicked($event)'>
</ai-star>
24
認識資料繫結方法 (Bindings)
• 內嵌繫結 ( interpolation )
{{property}}
• 屬性繫結 ( Property Binding )
[property]='statement'
• 事件繫結 ( Event Binding )
(event)='someMethod($event)'
• 雙向繫結 ( Two-way Binding )
[(ngModel)]='property'
25
透過 TypeScript 加強元件結構
• 使用型別標註 ( Type annotations ) 強化工具支援
• 使用「介面」強化工具支援 ( Strong typing )
• 使用「介面」確保 Lifecycle hooks 可以正確撰寫
• 使用泛型 ( Generic ) 建立程式碼範本
• 使用列舉 ( Enum ) 增加程式可讀性
26
相關連結
• Angular 2 官網
• Angular 2 學習資源
• Angular 2 風格指南 (官方版)
• Angular Augury (開發偵錯工具) (GitHub)
• codelyzer (程式碼風格分析器)
• Angular CLI (命令列工具)
• Angular 2 Cheat Sheet for TypeScript
• ng-conf 2016 – YouTube
• ReactiveX ( RxJS on GitHub )
• RxMarbles: Interactive diagrams of Rx Observables
• TypeScript - JavaScript that scales.
• TypeScript Handbook (中文版) 27
聯絡資訊
• The Will Will Web
記載著 Will 在網路世界的學習心得與技術分享
– http://blog.miniasp.com/
• Will 保哥的技術交流中心 (臉書粉絲專頁)
– http://www.facebook.com/will.fans
• Will 保哥的噗浪
– http://www.plurk.com/willh/invite
• Will 保哥的推特
– https://twitter.com/Will_Huang

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Advanced angular
Advanced angularAdvanced angular
Advanced angular
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Webpack slides
Webpack slidesWebpack slides
Webpack slides
 
Angular Taiwan 2019 - 大型 Angular 專案的的管理心得與技巧
Angular Taiwan 2019 - 大型 Angular 專案的的管理心得與技巧Angular Taiwan 2019 - 大型 Angular 專案的的管理心得與技巧
Angular Taiwan 2019 - 大型 Angular 專案的的管理心得與技巧
 
Git
GitGit
Git
 
Github
GithubGithub
Github
 
ePub 3, HTML 5 & CSS 3 (+ Fixed-Layout)
ePub 3, HTML 5 & CSS 3 (+ Fixed-Layout)ePub 3, HTML 5 & CSS 3 (+ Fixed-Layout)
ePub 3, HTML 5 & CSS 3 (+ Fixed-Layout)
 
Difference Between Angular and AngularJS.pdf
Difference Between Angular and AngularJS.pdfDifference Between Angular and AngularJS.pdf
Difference Between Angular and AngularJS.pdf
 
Vue JS Intro
Vue JS IntroVue JS Intro
Vue JS Intro
 
Angular kickstart slideshare
Angular kickstart   slideshareAngular kickstart   slideshare
Angular kickstart slideshare
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
Basics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfBasics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdf
 
Web development with django - Basics Presentation
Web development with django - Basics PresentationWeb development with django - Basics Presentation
Web development with django - Basics Presentation
 
Gstreamer Basics
Gstreamer BasicsGstreamer Basics
Gstreamer Basics
 
Django Architecture Introduction
Django Architecture IntroductionDjango Architecture Introduction
Django Architecture Introduction
 
Intro to Git and GitHub
Intro to Git and GitHubIntro to Git and GitHub
Intro to Git and GitHub
 
WebLogic Scripting Tool Overview
WebLogic Scripting Tool OverviewWebLogic Scripting Tool Overview
WebLogic Scripting Tool Overview
 
Maven ppt
Maven pptMaven ppt
Maven ppt
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practices
 
Java logging
Java loggingJava logging
Java logging
 

Destaque

從開發人員角度十分鐘理解區塊鏈技術
從開發人員角度十分鐘理解區塊鏈技術從開發人員角度十分鐘理解區塊鏈技術
從開發人員角度十分鐘理解區塊鏈技術Will Huang
 
[E2E Testing] 一鍵入手 E2E
[E2E Testing] 一鍵入手 E2E[E2E Testing] 一鍵入手 E2E
[E2E Testing] 一鍵入手 E2EIvan Wei
 
Breaking Down Bitcoin - Sean Walsh - Los Angeles
Breaking Down Bitcoin - Sean Walsh - Los AngelesBreaking Down Bitcoin - Sean Walsh - Los Angeles
Breaking Down Bitcoin - Sean Walsh - Los AngelesSean Walsh
 
Bitcoin Protocols 1.0 and 2.0 Explained in the Series: Blockchain: The Inform...
Bitcoin Protocols 1.0 and 2.0 Explained in the Series: Blockchain: The Inform...Bitcoin Protocols 1.0 and 2.0 Explained in the Series: Blockchain: The Inform...
Bitcoin Protocols 1.0 and 2.0 Explained in the Series: Blockchain: The Inform...Melanie Swan
 
Bitcoin & Blockchain for Friends
Bitcoin & Blockchain for FriendsBitcoin & Blockchain for Friends
Bitcoin & Blockchain for FriendsSam Wouters
 
What is Bitcoin - The Internet of Money
What is Bitcoin - The Internet of MoneyWhat is Bitcoin - The Internet of Money
What is Bitcoin - The Internet of MoneyJuan Aziz
 
BlockChain, Bitcoin and Smart Contracts - Oleg Kudrenko
BlockChain, Bitcoin and Smart Contracts - Oleg KudrenkoBlockChain, Bitcoin and Smart Contracts - Oleg Kudrenko
BlockChain, Bitcoin and Smart Contracts - Oleg KudrenkoOleg Kudrenko
 
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)Will Huang
 
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具Will Huang
 
快速上手 Windows Containers 容器技術 (Docker Taipei)
快速上手 Windows Containers 容器技術 (Docker Taipei)快速上手 Windows Containers 容器技術 (Docker Taipei)
快速上手 Windows Containers 容器技術 (Docker Taipei)Will Huang
 
Azure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
Azure Web App on Linux @ Global Azure Bootcamp 2017 TaiwanAzure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
Azure Web App on Linux @ Global Azure Bootcamp 2017 TaiwanWill Huang
 
Growth Mindset 經驗分享
Growth Mindset 經驗分享Growth Mindset 經驗分享
Growth Mindset 經驗分享Will Huang
 
git merge 與 rebase 的觀念與實務應用
git merge 與 rebase 的觀念與實務應用git merge 與 rebase 的觀念與實務應用
git merge 與 rebase 的觀念與實務應用Will Huang
 
ASP.NET 5 的創新與變革
ASP.NET 5 的創新與變革ASP.NET 5 的創新與變革
ASP.NET 5 的創新與變革Will Huang
 
中小企業選擇雲端服務的實戰密技
中小企業選擇雲端服務的實戰密技中小企業選擇雲端服務的實戰密技
中小企業選擇雲端服務的實戰密技Will Huang
 
DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略Will Huang
 
初學者都該了解的 HTTP 通訊協定基礎
初學者都該了解的 HTTP 通訊協定基礎初學者都該了解的 HTTP 通訊協定基礎
初學者都該了解的 HTTP 通訊協定基礎Will Huang
 
SQL Server 資料庫版本控管
SQL Server 資料庫版本控管SQL Server 資料庫版本控管
SQL Server 資料庫版本控管Will Huang
 
簡介 Git hub 平台 ( 1.5 hrs )
簡介 Git hub 平台 ( 1.5 hrs )簡介 Git hub 平台 ( 1.5 hrs )
簡介 Git hub 平台 ( 1.5 hrs )Will Huang
 
Visual Studio 2017 新功能探索 (Study4.TW)
Visual Studio 2017 新功能探索 (Study4.TW)Visual Studio 2017 新功能探索 (Study4.TW)
Visual Studio 2017 新功能探索 (Study4.TW)Will Huang
 

Destaque (20)

從開發人員角度十分鐘理解區塊鏈技術
從開發人員角度十分鐘理解區塊鏈技術從開發人員角度十分鐘理解區塊鏈技術
從開發人員角度十分鐘理解區塊鏈技術
 
[E2E Testing] 一鍵入手 E2E
[E2E Testing] 一鍵入手 E2E[E2E Testing] 一鍵入手 E2E
[E2E Testing] 一鍵入手 E2E
 
Breaking Down Bitcoin - Sean Walsh - Los Angeles
Breaking Down Bitcoin - Sean Walsh - Los AngelesBreaking Down Bitcoin - Sean Walsh - Los Angeles
Breaking Down Bitcoin - Sean Walsh - Los Angeles
 
Bitcoin Protocols 1.0 and 2.0 Explained in the Series: Blockchain: The Inform...
Bitcoin Protocols 1.0 and 2.0 Explained in the Series: Blockchain: The Inform...Bitcoin Protocols 1.0 and 2.0 Explained in the Series: Blockchain: The Inform...
Bitcoin Protocols 1.0 and 2.0 Explained in the Series: Blockchain: The Inform...
 
Bitcoin & Blockchain for Friends
Bitcoin & Blockchain for FriendsBitcoin & Blockchain for Friends
Bitcoin & Blockchain for Friends
 
What is Bitcoin - The Internet of Money
What is Bitcoin - The Internet of MoneyWhat is Bitcoin - The Internet of Money
What is Bitcoin - The Internet of Money
 
BlockChain, Bitcoin and Smart Contracts - Oleg Kudrenko
BlockChain, Bitcoin and Smart Contracts - Oleg KudrenkoBlockChain, Bitcoin and Smart Contracts - Oleg Kudrenko
BlockChain, Bitcoin and Smart Contracts - Oleg Kudrenko
 
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
使用 Angular 2 與 Firebase 實現 Serverless 網站架構 (JSDC.tw 2016)
 
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具
《保哥線上講堂》打造一個具有 Linux 溫度的 Windows 命令提示字元工具
 
快速上手 Windows Containers 容器技術 (Docker Taipei)
快速上手 Windows Containers 容器技術 (Docker Taipei)快速上手 Windows Containers 容器技術 (Docker Taipei)
快速上手 Windows Containers 容器技術 (Docker Taipei)
 
Azure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
Azure Web App on Linux @ Global Azure Bootcamp 2017 TaiwanAzure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
Azure Web App on Linux @ Global Azure Bootcamp 2017 Taiwan
 
Growth Mindset 經驗分享
Growth Mindset 經驗分享Growth Mindset 經驗分享
Growth Mindset 經驗分享
 
git merge 與 rebase 的觀念與實務應用
git merge 與 rebase 的觀念與實務應用git merge 與 rebase 的觀念與實務應用
git merge 與 rebase 的觀念與實務應用
 
ASP.NET 5 的創新與變革
ASP.NET 5 的創新與變革ASP.NET 5 的創新與變革
ASP.NET 5 的創新與變革
 
中小企業選擇雲端服務的實戰密技
中小企業選擇雲端服務的實戰密技中小企業選擇雲端服務的實戰密技
中小企業選擇雲端服務的實戰密技
 
DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略DEV305 - ASP.NET 5 開發攻略
DEV305 - ASP.NET 5 開發攻略
 
初學者都該了解的 HTTP 通訊協定基礎
初學者都該了解的 HTTP 通訊協定基礎初學者都該了解的 HTTP 通訊協定基礎
初學者都該了解的 HTTP 通訊協定基礎
 
SQL Server 資料庫版本控管
SQL Server 資料庫版本控管SQL Server 資料庫版本控管
SQL Server 資料庫版本控管
 
簡介 Git hub 平台 ( 1.5 hrs )
簡介 Git hub 平台 ( 1.5 hrs )簡介 Git hub 平台 ( 1.5 hrs )
簡介 Git hub 平台 ( 1.5 hrs )
 
Visual Studio 2017 新功能探索 (Study4.TW)
Visual Studio 2017 新功能探索 (Study4.TW)Visual Studio 2017 新功能探索 (Study4.TW)
Visual Studio 2017 新功能探索 (Study4.TW)
 

Semelhante a 使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例

快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)Will Huang
 
Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Will Huang
 
ASP.NET MVC 6 新功能探索
ASP.NET MVC 6 新功能探索ASP.NET MVC 6 新功能探索
ASP.NET MVC 6 新功能探索Will Huang
 
Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸Will Huang
 
ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索Will Huang
 
Kindeditor 设计思路
Kindeditor 设计思路Kindeditor 设计思路
Kindeditor 设计思路luolonghao
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVCjeffz
 
微服務架構 導入經驗分享 吳剛志 - Community Open Camp
微服務架構 導入經驗分享 吳剛志 - Community Open Camp微服務架構 導入經驗分享 吳剛志 - Community Open Camp
微服務架構 導入經驗分享 吳剛志 - Community Open CampAndrew Wu
 
合久必分,分久必合
合久必分,分久必合合久必分,分久必合
合久必分,分久必合Qiangning Hong
 
Kind editor设计思路
Kind editor设计思路Kind editor设计思路
Kind editor设计思路taobao.com
 
浏览器渲染与web前端开发
浏览器渲染与web前端开发浏览器渲染与web前端开发
浏览器渲染与web前端开发Duoyi Wu
 
從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境Will Huang
 
KISSY Editor Design 2
KISSY Editor Design 2KISSY Editor Design 2
KISSY Editor Design 2yiming he
 
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器Chieh Kai Yang
 
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛Edward Kuo
 
ASP.NET Core 2.1設計新思維與新發展
ASP.NET  Core 2.1設計新思維與新發展ASP.NET  Core 2.1設計新思維與新發展
ASP.NET Core 2.1設計新思維與新發展江華 奚
 
Kindeditor设计思路v2
Kindeditor设计思路v2Kindeditor设计思路v2
Kindeditor设计思路v2luolonghao
 
美团前端架构简介
美团前端架构简介美团前端架构简介
美团前端架构简介pan weizeng
 
钱宝坤:多浏览器集成的JavaScript单元测试工具
钱宝坤:多浏览器集成的JavaScript单元测试工具钱宝坤:多浏览器集成的JavaScript单元测试工具
钱宝坤:多浏览器集成的JavaScript单元测试工具taobao.com
 

Semelhante a 使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例 (20)

快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
快快樂樂學會 Angular 2 網站開發框架 (Modern Web 2016)
 
Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)Angular 7 全新功能探索 (Angular Taiwan 2018)
Angular 7 全新功能探索 (Angular Taiwan 2018)
 
ASP.NET MVC 6 新功能探索
ASP.NET MVC 6 新功能探索ASP.NET MVC 6 新功能探索
ASP.NET MVC 6 新功能探索
 
Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸
 
ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索
 
Kindeditor 设计思路
Kindeditor 设计思路Kindeditor 设计思路
Kindeditor 设计思路
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVC
 
微服務架構 導入經驗分享 吳剛志 - Community Open Camp
微服務架構 導入經驗分享 吳剛志 - Community Open Camp微服務架構 導入經驗分享 吳剛志 - Community Open Camp
微服務架構 導入經驗分享 吳剛志 - Community Open Camp
 
合久必分,分久必合
合久必分,分久必合合久必分,分久必合
合久必分,分久必合
 
Kind editor设计思路
Kind editor设计思路Kind editor设计思路
Kind editor设计思路
 
浏览器渲染与web前端开发
浏览器渲染与web前端开发浏览器渲染与web前端开发
浏览器渲染与web前端开发
 
從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境從頭打造 C#、.NET 與 ASP.NET Core 開發環境
從頭打造 C#、.NET 與 ASP.NET Core 開發環境
 
KISSY Editor Design 2
KISSY Editor Design 2KISSY Editor Design 2
KISSY Editor Design 2
 
Mvc model
Mvc modelMvc model
Mvc model
 
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
 
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
[2020 .NET Conf] 企業Azure DevOps Service 實際應用架構與秘辛
 
ASP.NET Core 2.1設計新思維與新發展
ASP.NET  Core 2.1設計新思維與新發展ASP.NET  Core 2.1設計新思維與新發展
ASP.NET Core 2.1設計新思維與新發展
 
Kindeditor设计思路v2
Kindeditor设计思路v2Kindeditor设计思路v2
Kindeditor设计思路v2
 
美团前端架构简介
美团前端架构简介美团前端架构简介
美团前端架构简介
 
钱宝坤:多浏览器集成的JavaScript单元测试工具
钱宝坤:多浏览器集成的JavaScript单元测试工具钱宝坤:多浏览器集成的JavaScript单元测试工具
钱宝坤:多浏览器集成的JavaScript单元测试工具
 

Mais de Will Huang

深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)Will Huang
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!Will Huang
 
你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧Will Huang
 
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)Will Huang
 
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)Will Huang
 
從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點Will Huang
 
RxJS 6 新手入門
RxJS 6 新手入門RxJS 6 新手入門
RxJS 6 新手入門Will Huang
 
极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)Will Huang
 
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)Will Huang
 
Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)Will Huang
 
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)Will Huang
 
Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Will Huang
 
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)Will Huang
 
AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)Will Huang
 
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)Will Huang
 
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)Will Huang
 
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)Will Huang
 
以敏捷架構打造美國軟體外包專案的經驗談
以敏捷架構打造美國軟體外包專案的經驗談以敏捷架構打造美國軟體外包專案的經驗談
以敏捷架構打造美國軟體外包專案的經驗談Will Huang
 
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018Will Huang
 
迎接嶄新的Windows容器叢集架構:Kubernetes
迎接嶄新的Windows容器叢集架構:Kubernetes迎接嶄新的Windows容器叢集架構:Kubernetes
迎接嶄新的Windows容器叢集架構:KubernetesWill Huang
 

Mais de Will Huang (20)

深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
深入理解 CVE-2022-24765 漏洞的攻擊與防護策略 (Git v2.35.2)
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
 
你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧
 
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
使用 .NET 5 實現美股期貨的量化交易策略 (.NET Conf 2020)
 
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
 
從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點從實戰經驗看到的 K8S 導入痛點
從實戰經驗看到的 K8S 導入痛點
 
RxJS 6 新手入門
RxJS 6 新手入門RxJS 6 新手入門
RxJS 6 新手入門
 
极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)极速 Angular 开发:效能调校技巧 (ngChina 2019)
极速 Angular 开发:效能调校技巧 (ngChina 2019)
 
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
你不可不知的 ASP.NET Core 3 全新功能探索 (.NET Conf 2019)
 
Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)Protractor: The Hacker way (NG-MY 2019)
Protractor: The Hacker way (NG-MY 2019)
 
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
邁向 Windows Server 應用程式現代化 (Windows Server Application Modernization)
 
Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)Angular 开发技巧 (2018 ngChina 开发者大会)
Angular 开发技巧 (2018 ngChina 开发者大会)
 
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
 
AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)AKS 與開發人員體驗 (Kubernetes 大講堂)
AKS 與開發人員體驗 (Kubernetes 大講堂)
 
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
使用 ASP.NET Blazor 開發 SPA 網頁應用程式 (.NET Conf 2018)
 
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)全新 Windows Server 2019 容器技術及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
全新 Windows Server 2019 容器技術 及邁向與 Kubernetes 整合之路 (Windows Server 高峰會)
 
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
 
以敏捷架構打造美國軟體外包專案的經驗談
以敏捷架構打造美國軟體外包專案的經驗談以敏捷架構打造美國軟體外包專案的經驗談
以敏捷架構打造美國軟體外包專案的經驗談
 
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
開發人員必須知道的 Kubernetes 核心技術 - Kubernetes Summit 2018
 
迎接嶄新的Windows容器叢集架構:Kubernetes
迎接嶄新的Windows容器叢集架構:Kubernetes迎接嶄新的Windows容器叢集架構:Kubernetes
迎接嶄新的Windows容器叢集架構:Kubernetes
 

使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例

  • 1. 使用 TypeScript 駕馭 Web 世界的脫韁野馬 多奇數位創意有限公司 技術總監 黃保翕 ( Will 保哥 ) 部落格:http://blog.miniasp.com/ 以 Angular 2 開發框架為例
  • 2. ANGULAR 2 簡介 Angular 2 Introduction
  • 3. 與 Angular 1.x 的比較 • 三大特色 – 效能改進 (Performance) • 偵測變更:比 ng1 快 10 倍 • 渲染速度:比 ng1 快 5 倍 (Render & Re-render) • 範本編譯:支援 Template 預先編譯機制 • 更小的 Library Size 與延遲載入機制 • 支援伺服器渲染機制 (Node.js & ASP.NET ) – 高生產力 (Productivity) • 開發應用程式能夠用更簡潔的語法讓團隊更加容易上手跟維護 • 更強大的開發工具 Augury • 移除超過 40+ 個 directives – 多樣平台 (Versatility) • 支援 Browser, Node.js, NativeScript, and more … 3
  • 6. Angular 2 的開發語言 • ES5 – 傳統 JavaScript 程式語言 ( IE9+ ) • ES 2015 – 此版本為 ES5 的「超集合」 – 具有新穎的 JavaScript 語言特性 ( let, const, for-of, … ) – 可透過 Babel 轉譯器將瀏覽器不支援的語法轉為 ES5 版本 • TypeScript – 此版本為 ES 2015 的「超集合」 – 具有強型別特性、內建 ES5 轉譯器 (Transpiler)、更好的工具支 援 • Dart – 非 JavaScript 家族的程式語言 – 具有強型別特性 6
  • 7. Angular 2 的開發工具 • Visual Studio Code • Visual Studio 2015 • Atom • Sublime Text • Plunker 7
  • 8. 建立第一個 ANGULAR 2 應用程式 Build your first Angular 2 Application 8
  • 9. Angular 2 應用程式的組成 • App Component元件 • Child Component元件 • Services Component元件 • Pipe Component元件
  • 10. Angular 2 頁面的組成 應用程式元件 + 樣板 ( AppComponent + Templates ) 頁首元件 + 樣板 ( HeaderComponent ) 子選單 元件 + 樣板 (SideComponent) 10 主要內容元件 + 樣板 (MainComponent)
  • 12. Angular 2 結構剖析 • Module 應用程式被切分成許多「模組」 • Component 每個模組下有許多「元件」 • Template 每個元件都可能有自己的「樣板」 • Metadata 每個元件都可以標示「中繼資料」 • Data Binding 樣板與元件屬性、方法可以進行綁定 • Directive 將 DOM 轉換為多功能的「宣告命令」 • Service 由「服務」集中管理資料與運算邏輯 • Dependency Injection 由「相依注入」機制管理物件生命週期 12
  • 13. Angular 2 元件的組成 範本 ( Template ) • HTML 版面配置 • HTML 部分片段 • 資料繫結 (Bindings) • 畫面命令 (Directives) 類別 ( Class ) • 建構式 (Constructor) • 屬性 (Properties) • 方法 (Methods) 中繼資料 ( Metadata ) • 裝飾器 (Decorator) • 針對類別 • 針對屬性 • 針對方法 • 針對參數 13
  • 14. 手動建立基礎開發環境 1. 建立應用程式資料夾 2. 建立 tsconfig.json 3. 建立 package.json 4. 建立 typings.json 5. 建立 libraries & typings 6. 建立 index.html 7. 建立 main.ts (bootstrapper) 8. 設定 .gitignore 與建立版控 9. 設定 Visual Studio Code 開發工具 ( .vscode ) 14
  • 15. 使用 Angular CLI 建立專案範本 • npm install -g angular-cli • ng new PROJECT_NAME • cd PROJECT_NAME • ng serve – http://localhost:4200/ • ng generate component my-new-component 縮寫語法: ng g component my-new-component 15
  • 16. 複製現有的 Angular 2 專案範本 • 現有的專案範本 – miniasp/angular-2-boilerplate – miniasp/angular-2-boilerplate-webpack – AngularClass/angular2-webpack-starter – angular/angular2-seed – DanWahlin/Angular2-JumpStart – johnpapa/angular2-tour-of-heroes 16
  • 17. 認識 ES 2015 / TypeScript 模組化技術 • 每個檔案都是一個「模組」( module ) • 每個模組內都可以「匯出」( export ) 公開的物件 • 匯出 export class Product { pageTitle = "Hello World"; } • 匯入 import { Product } from './product'; import { Product as prod } from './product'; import * as product from './product'; import './product'; 17
  • 18. 建立第一個元件 (根元件) • 建立根元件 app/app.component.ts export class AppComponent { pageTitle: string = 'Hello World'; } • 匯入 Angular 2 啟動器 app/main.ts import { bootstrap } from '@angular/platform-browser-dynamic'; import { AppComponent } from './app.component'; bootstrap(AppComponent); 18
  • 19. 認識 Angular 2 元件的命名規則 // 命名規則: PascalCase export class AppComponent { // 命名規則: camelCase pageTitle : string = "Hello World"; // 命名規則: 動詞 + 名詞 with camelCase printTitle() { console.log(this.pageTitle); } } 19
  • 20. 認識 ES7 / TypeScript 的 Decorator • 可以套用在 – 類別 – 屬性 – 方法 – 方法中的參數 • 永遠以 @ 開頭 • 不用分號結尾 • 用法跟 C# 的 Attributes 很像!! 20
  • 21. 修改首頁 HTML 內容 • 修改 index.html <my-app> Loading App... </my-app> 21
  • 22. 建立第一個元件的 HTML 範本 import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: ` <div> <div class='container'><h1>{{pageTitle}}</h1></div> </div> ` }) export class AppComponent { pageTitle: string = 'Hello World'; } 22
  • 23. 建立子元件 • 建立模組檔案 ( *.ts ) star.component.ts • 建立元件類別 (Class) 與 中繼資料 (Decorator) @Component({ selector: 'posts', template: `xxxxx` }) export class StarComponent { } • 設定元件範本 (Template) star.component.html • 套用結構式命令 (Structure Directives) *ngIf='products && products.length' *ngFor='let product of products' 23
  • 24. 載入子元件 • 修改上層元件的 directives @Component({ templateUrl: 'app/products/product-list.component.html', directives: [StarComponent] }) • app/products/product-list.component.html <ai-star [rating]='product.starRating' (ratingClicked)='onRatingClicked($event)'> </ai-star> 24
  • 25. 認識資料繫結方法 (Bindings) • 內嵌繫結 ( interpolation ) {{property}} • 屬性繫結 ( Property Binding ) [property]='statement' • 事件繫結 ( Event Binding ) (event)='someMethod($event)' • 雙向繫結 ( Two-way Binding ) [(ngModel)]='property' 25
  • 26. 透過 TypeScript 加強元件結構 • 使用型別標註 ( Type annotations ) 強化工具支援 • 使用「介面」強化工具支援 ( Strong typing ) • 使用「介面」確保 Lifecycle hooks 可以正確撰寫 • 使用泛型 ( Generic ) 建立程式碼範本 • 使用列舉 ( Enum ) 增加程式可讀性 26
  • 27. 相關連結 • Angular 2 官網 • Angular 2 學習資源 • Angular 2 風格指南 (官方版) • Angular Augury (開發偵錯工具) (GitHub) • codelyzer (程式碼風格分析器) • Angular CLI (命令列工具) • Angular 2 Cheat Sheet for TypeScript • ng-conf 2016 – YouTube • ReactiveX ( RxJS on GitHub ) • RxMarbles: Interactive diagrams of Rx Observables • TypeScript - JavaScript that scales. • TypeScript Handbook (中文版) 27
  • 28. 聯絡資訊 • The Will Will Web 記載著 Will 在網路世界的學習心得與技術分享 – http://blog.miniasp.com/ • Will 保哥的技術交流中心 (臉書粉絲專頁) – http://www.facebook.com/will.fans • Will 保哥的噗浪 – http://www.plurk.com/willh/invite • Will 保哥的推特 – https://twitter.com/Will_Huang