Angular2 從搭建環境到開發工作筆記

來源:互聯網
上載者:User


Angular2 的發布帶來了一陣熱議,很久之前就在籌備了,當時的官方回覆就是徹底推翻重寫,問世之後大家的呼聲就是學習成本太高,雖然去掉了 1.x 裡的一部分概念,但是加進了 typescript,雖然不強制使用,但是我推薦大家都試一試,畢竟此次改版是Google和微軟兩大家的產物。

對於會部署環境的可以嘗試本文最後一節加入 Angular material2 ,個人認為對高度個人化的項目不推薦使用,對企業級的 CMS 省去了寫樣式的時間,直接開始本文。

Angular-CLI

說到 cli 大家不陌生,沒出一個架構都會有對應的 cli ,俗稱腳手架。angular2 本身提供了起步項目 angular2-quickstart,我嘗試了一下,發現不是很好用,其它的大部分擴充需要自行安裝,之後看了一下 angular-cli 部署簡單易用,還提供了快捷搭建項目的目錄。

Github地址: https://github.com/angular/angular-cli

我就簡單說下 Github 裡的文檔吧,細部的大家擴充閱讀。

安裝

首先,最好先升級 node 到 6.x 可以避免 node 版本過低帶來的不必要的麻煩。

npm install -g angular-cli

用法

ng --help

查看所有用法

建立本地開發環境產生和運行angular2項目

ng new PROJECT_NAME
cd PROJECT_NAME
ng serve

PROJECT_NAME 是你自己的項目名

部署成功後不報錯的情況下到瀏覽器 http://localhost:4200/,修改項目中檔案後會自動部署

您可以配置預設的 HTTP 連接埠和一個 LiveReload server 用 --, 形如:

ng serve --host 0.0.0.0 --port 4201 --live-reload-port 49153

產生組件、指令、管道和服務

命令以 ng generate 開頭,可以縮寫為 ng g,下面給出建立 component 的幾種方式。

ng generate component my-new-component
ng g component my-new-component # using the alias

# components support relative path generation
# if in the directory src/app/feature/ and you run
ng g component new-cmp
# your component will be generated in src/app/feature/new-cmp
# but if you were to run
ng g component ../newer-cmp
# your component will be generated in src/app/newer-cmp

下表裡是所有的命令:

Scaffold Usage
Component ng g component my-new-component
Directive ng g directive my-new-directive
Pipe ng g pipe my-new-pipe
Service ng g service my-new-service
Class ng g class my-new-class
Interface ng g interface my-new-interface
Enum ng g enum my-new-enum
Module ng g module my-module

建立路由

這裡 cli 暫時禁用了建立路由,新的路由產生器即將到來,您可以在這裡閱讀新路由器的官方文檔:https://angular.io/docs/ts/latest/guide/router.html

建立一個 build

ng build

會產生到 dist/ 目錄下,其它關於測試,設定檔請大家去 Github 仔細閱讀,這裡只給最基本的搭建流程。

組件實戰

看到這你可能已經開始嘗試了,建立項目的步驟相信大家參照上文可以輕鬆解決,這裡我先嘗試建立一個 component,命令如下。

ng g component nav

這裡我建立了一個 nav 組件。執行成功後,後台會自動部署。我們看一下檔案目錄有什麼改變

 

多了一個叫做 nav 的檔案夾,看一看檔案目錄


我們發現與項目建立時內建的 app component 目錄結構相同,內容也大同小異,大家可以嘗試去建立一個自己的組件,組件的樣式可以去對應的 css 檔案中修改。

這時我的 app.module.ts 變成了如下

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { NavComponent } from './nav/nav.component';

@NgModule({
  declarations: [
    AppComponent,
    NavComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
這裡不難看出全域自動引入了 nav.component 組件。我們現在關心的問題是組件之間的引用和資料轉送,這裡為了簡單起見,只給引入的方法,而資料轉送、路由機制這裡不做解釋大家自行官網。

下面說一下 app 內引入 nav 組件,只需要改變 app.component.html 如下。

<h1 class="title">
  {{title}}
</h1>

<app-nav></app-nav>

這裡的 class 在對應的 app.component.css 如下


.title {
  font-size: 100px;
}
這時頁面自動重新整理字型大小變大 ,那麼這裡的 app-nav 標籤從哪裡得到的呢?

我們去 nav.component.ts 裡看一眼

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

@Component({
  selector: 'app-nav',
  templateUrl: './nav.component.html',
  styleUrls: ['./nav.component.css']
})
export class NavComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}
這裡的 selector: 'app-nav' 說明我們的選取器選擇的是 app-nav 標籤,同樣的可以通過 [app-nav] 選擇屬性。

註:這裡 selector 類似 css 中的選取器,大家也可以根據 1.x 中的 directive 來理解這裡的組件
此時頁面會呈現成這樣

 

好,到這裡簡單的組件引用已經實現。

引入 Angular material2

文章開頭已經闡述了引入 Angular material2 的優點,用過其它組件樣式架構的都明白。

安裝命令

npm install --save @angular/material

在 src/app/app.module.ts 中引入架構


import { MaterialModule } from '@angular/material';
// other imports
@NgModule({
  imports: [MaterialModule.forRoot()],
  ...
})
export class PizzaPartyAppModule { }
引入核心和主體風格,較 Angular material 1.x 的改進在於可以選擇不同的色系。具體看文檔連結:https://github.com/angular/material2/blob/master/docs/theming.md

我們這裡用的是 Angular CLI 這裡又可以鑽空子啦,添加下面一行到 style.css,注意是 src 目錄下的檔案


@import '~@angular/material/core/theming/prebuilt/deeppurple-amber.css';

deeppurple-amber 佈景主題色彩是可變的,具體看上文的文檔連結。

到這裡一直開啟控制台(是個好習慣)的朋友會發現類似下面的報錯。


client:49 [default] J:\workspace\angular2\ts\epimss\node_modules\@angular2-material\slide-toggle\slide-toggle.d.ts:67:19
Cannot find name 'HammerInput'.

client:49 [default] J:\workspace\angular2\ts\epimss\node_modules\@angular2-material\core\gestures\MdGestureConfig.d.ts:4:39
Cannot find name 'HammerManager'.
文檔也給出瞭解釋,因為架構中 md-slide-toggle 和 md-slider 兩個組件依賴外部第三方組件 HammerJS 需要額外的配置。

我們不急著用文檔給的 npm 或引入 cdn 路徑,因為親測還是會報錯,可能我引入方式有誤,為了大家少走彎路直接給親測有效方法

我們先去命令列工具運行 npm i --save-dev @types/hammerjs

然後編輯 tsconfig.json 檔案將 hammerjs 添加到 types 下

"types": [
  "jasmine", "hammerjs"
]

到這裡發現頁面自動重新整理後報錯消失了,如果需要字型表徵圖可以在 src/index.html 中引入


<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
 rel="stylesheet">
目前為止,整個 Angular material2 已經整裝待發。全部文法看這裡:https://github.com/angular/material2#feature-status

我們嘗試著添加多個按鈕組件測試一下,修改 app.component.html 檔案,完整代碼如下

<h1 class="title">
  {{title}}
</h1>
<app-nav></app-nav>

<button md-button>FLAT</button>
<button md-raised-button>RAISED</button>
<button md-icon-button>
  <md-icon class="md-24">favorite</md-icon>
</button>
<button md-fab>
  <md-icon class="md-24">add</md-icon>
</button>
<button md-mini-fab>
  <md-icon class="md-24">add</md-icon>
</button>
<br/>
<br/>

<button md-raised-button color="primary">PRIMARY</button>
<button md-raised-button color="accent">ACCENT</button>
<button md-raised-button color="warn">WARN</button>
<br/>
<br/>

<button md-button disabled>OFF</button>
<button md-raised-button [disabled]="isDisabled">OFF</button>
<button md-mini-fab [disabled]="isDisabled"><md-icon>check</md-icon></button>

沒問題這裏手懶不寫配置樣式了,直接給 br 換行大家方便看些,待頁面部署完成後我們會看到以下效果

 

炫酷的組件,更多組件文法參考上面給的連結,到這裡相信大家學習 angular2 的信心倍增,真對已有組件可以完成快速開發,下一步就是大家去 Angular2 官網看其它概念的時候啦,處理資料實現與後端對接。項目上線,大功告成。

總結

orange 最近也是在學習新技術,更底層架構方面的知識還再學習,當今前端架構層出不窮,不要盲從,要根據公司需求和員工的工作經驗選擇架構,真說到效能方面哪個架構快的話,我雖然沒測試過,但我確定 React、Vue、Angular2 幾個之間相差無幾,除非在實現的時候代碼存在問題,因為這幾個架構都經過了大型項目的考驗。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.