Angular 2 TypeScript 環境配置
本章節使用的是 TypeScript 來建立 Angular 的應用,這也是官方推薦使用的,本教程的例項也將採用 TypeScript 來編寫。
TypeScript 是一種由微軟開發的自由和開源的程式語言,它是JavaScript的一個超集,擴充套件了JavaScript的語法。
如果你不瞭解TypeScript,可以查閱以下資料:
這開始前,你需要確保你已經安裝了 npm,如果你還沒安裝npm或者不瞭解 npm 可以檢視我們的教程:NPM 使用介紹。
由於 npm 官網映象國內訪問太慢,這裡我使用了淘寶的npm映象,安裝方法如下:
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
執行後我們就可以使用 cnpm 命令來安裝模組:
$ cnpm install
第一步:建立與配置專案
建立目錄
$ mkdir angular-quickstart $ cd angular-quickstart
建立配置檔案
Angular 專案需要以下幾個配置檔案:
- package.json 標記本專案所需的 npm 依賴包。
- tsconfig.json 定義了 TypeScript 編譯器如何從專案原始檔生成 JavaScript 程式碼。
- typings.json為那些 TypeScript 編譯器無法識別的庫提供了額外的定義檔案。
- systemjs.config.js 為模組載入器提供了該到哪裡查詢應用模組的資訊,並註冊了所有必備的依賴包。 它還包括文件中後面的例子需要用到的包。
在 angular-quickstart 中建立以下幾個檔案,程式碼如下所示:
package.json 檔案:
{"name": "angular-quickstart", "version": "1.0.0", "scripts": {"start": "tsc && concurrently \"npm run tsc:w\"\"npm run lite\"", "lite": "lite-server", "postinstalltsconfig.json 檔案:
{"compilerOptions": {"target": "es5", "module": "commonjs", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false}}typings.json 檔案:
{"globalDependencies": {"core-js": "registry:dt/core-js#0.0.0+20160725163759", "jasmine": "registry:dt/jasmine#2.2.0+20160621224255", "node": "registry:dt/node#6.0.0+20160909174046"}}systemjs.config.js 檔案:
/** * System configuration for Angular samples * Adjust as necessary for your application needs. */(function(global){System.config({paths: {// paths serve as alias'npm:': 'node_modules/'}, // map tells the System loader where to look for thingsmap: {// our app is within the app folderapp: 'app', // angular bundles'@angular/core': 'npm:@angular/core/bundles/core.umd.js', '@angular/common': 'npm:@angular/common/bundles/common.umd.js', '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', '@angular/http': 'npm:@angular/http/bundles/http.umd.js', '@angular/router': 'npm:@angular/router/bundles/router.umd.js', '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', // other libraries'rxjs': 'npm:rxjs', 'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api', }, // packages tells the System loader how to load when no filename and/or no extensionpackages: {app: {main: './main.js', defaultExtension: 'js'}, rxjs: {defaultExtension: 'js'}, 'angular2-in-memory-web-api': {main: './index.js', defaultExtension: 'js'}}});})(this);接下來我們使用 cnpm 命令來安裝依賴包:
$ cnpm install
執行成功後,angular-quickstart 目錄下就會生成一個 node_modules 目錄,這裡包含了我們這個例項需要的模組,我們可以看下專案的目錄結構:
第二步:建立應用
我們用 NgModules 把 Angular 應用組織成了一些功能相關的程式碼塊。
Angular 本身是被拆成一些獨立的 Angular 模組,這樣我們在應用中只需要匯入需要的 Angular 部分。
每個 Angular 應用至少需要一個root module(根模組) ,例項中為 AppModule 。
接下來我們在 angular-quickstart 目錄下建立 app 目錄:
$ mkdir app
$ cd app
然後在 app 目錄下建立 app.module.ts 檔案,程式碼如下所示:
app.module.ts 檔案:
import{NgModule}from'@angular/core';import{BrowserModule}from'@angular/platform-browser'; @NgModule({imports: [BrowserModule]})exportclassAppModule{}由於 QuickStart 是一個執行在瀏覽器中的 Web 應用,所以根模組需要從 @angular/platform-browser 中匯入 BrowserModule 並新增到 imports 陣列中。
建立元件並新增到應用中
每個 Angular 應用都至少有一個根元件, 例項中為 AppComponent,app.component.ts 檔案程式碼如下:
app.component.ts 檔案:
import{Component}from'@angular/core';@Component({selector: 'my-app', template: '<h1>我的第一個 Angular 應用</h1>'})exportclassAppComponent{}程式碼解析:
-
以上程式碼從 angular2/core 引入了 Component 包。
-
@Component 是 Angular 2 的裝飾器 ,它會把一份元資料關聯到 AppComponent 元件類上。
-
my-app 是一個 CSS 選擇器,可用在 HTML 標籤中,作為一個元件使用。
-
@view 包含了一個 template ,告訴 Angular 如何渲染該元件的檢視。
-
export 指定了元件可以再檔案外使用。
接下來我們重新開啟 app.module.ts 檔案,匯入新的 AppComponent ,並把它新增到 NgModule 裝飾器的 declarations 和 bootstrap 欄位中: