認識 Angular 元件的程式碼結構 import{ Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrls: ['app.component.css'] }) export class AppComponent { title = 'app works!'; } 14 類別 匯入模組 裝飾器
15.
使用 Angular CLI快速產生程式碼 • 透過 藍圖 (blueprint) 產生程式碼 – ng generate 藍圖 元件名稱 • 透過 藍圖 (blueprint) 產生程式碼 (簡寫) – ng g 藍圖 元件名稱 • 使用範例 – 產生 HeaderComponent 元件 • ng g component header # 建立元件 • ng g c header # 簡寫版本 ( c = component ) • ng g c header --spec=false # 不要建立單元測試檔 ( *.spec.ts ) • ng g c charts/header # 在特定目錄(模組)下建立元件 – 產生 DataService 服務元件 • ng g s data # 建立服務元件 – 產生 Charts 模組 • ng g m charts # 建立模組 – 查詢其他藍圖用法 • ng g --help # 顯示所有藍圖與用法說明
16.
常見 Angular CLI產生器藍圖與範例 16 藍圖名稱 使用方式 Component ng g component my-new-component Service ng g service my-new-service Module ng g module my-module Directive ng g directive my-new-directive Pipe ng g pipe my-new-pipe Class ng g class my-new-class Interface ng g interface my-new-interface Enum ng g enum my-new-enum
聯絡資訊 • The WillWill Web 記載著 Will 在網路世界的學習心得與技術分享 – http://blog.miniasp.com/ • Will 保哥的技術交流中心 (臉書粉絲專頁) – http://www.facebook.com/will.fans • Will 保哥的推特 – https://twitter.com/Will_Huang • Will 保哥的噗浪 – http://www.plurk.com/willh/invite