1.建立文件夾javascript
mkdir angular2-app
cd angular2-app
2.配置Typescripthtml
須要經過一些特殊的設置來指導Typesript進行編譯。
新建一個 tsconfig.json
文件,放於項目根目錄下,並輸入如下配置java
{ "compilerOptions": { "target": "es5", "module": "system", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false }, "exclude": [ "node_modules", "typings/main", "typings/main.d.ts" ] }
3.Typescript Typingsnode
有不少Javascript的庫,繼承了一些 Javascript的環境變量以及語法, Typescript編譯器並不能原生的支持這些。 因此咱們使用 Typescript 類型定義文件 - d.ts
文件 (即 typings.json) 來解決這些兼容性問題。git
建立 typings.json
文件,放於項目根目錄下es6
{ "ambientDependencies": { "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2" } }
4.添加咱們須要的庫github
咱們推薦使用npm來管理咱們的依賴庫。在項目根目錄下建立package.json文件typescript
{ "name": "angular2-quickstart", "version": "1.0.0", "scripts": { "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ", "tsc": "tsc", "tsc:w": "tsc -w", "lite": "lite-server", "typings": "typings", "postinstall": "typings install" }, "license": "ISC", "dependencies": { "angular2": "2.0.0-beta.7", "systemjs": "0.19.22", "es6-promise": "^3.0.2", "es6-shim": "^0.33.3", "reflect-metadata": "0.1.2", "rxjs": "5.0.0-beta.2", "zone.js": "0.5.15" }, "devDependencies": { "concurrently": "^2.0.0", "lite-server": "^2.1.0", "typescript": "^1.7.5", "typings":"^0.6.8" } }
5.安裝這些依賴包只須要運行npm
npm install
這樣咱們就完成了咱們的開發環境的搭建。json
6.建立一個應用源碼的子目錄
咱們習慣上將咱們的程序放在項目根目錄下的 app 子目錄下,因此首先建立一個 app 文件夾
mkdir app
cd app
7.建立組件文件
在 app 文件夾下建立一個 app.component.ts 文件,而後輸入如下內容
import {Component} from 'angular2/core'; @Component({ selector: 'my-app', template: '<h1>My First Angular 2 App</h1>' }) export class AppComponent { }
讓咱們來詳細的看一下這個文件, 在文件的最後一行,咱們定義了一個 類。
8.初始化引導
在 app 文件夾下建立 main.ts
import {bootstrap} from 'angular2/platform/browser' import {AppComponent} from './app.component' bootstrap(AppComponent);
咱們須要作兩個東西來啓動這個應用
Angular自帶的 bootstrap 方法
咱們剛剛寫好的啓動組件
把這個兩個通通 import進來,而後將組件傳遞給 bootstrap 方法。
9.添加 index.html 文件
首先回到項目的根目錄,在根目錄中建立index.html
<html> <head> <title>Angular 2 QuickStart</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 1. Load libraries --> <!-- IE required polyfills, in this exact order --> <script src="node_modules/es6-shim/es6-shim.min.js"></script> <script src="node_modules/systemjs/dist/system-polyfills.js"></script> <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="node_modules/rxjs/bundles/Rx.js"></script> <script src="node_modules/angular2/bundles/angular2.dev.js"></script> <!-- 2. Configure SystemJS --> <script> System.config({ packages: { app: { format: 'register', defaultExtension: 'js' } } }); System.import('app/main') .then(null, console.error.bind(console)); </script> </head> <!-- 3. Display the application --> <body> <my-app>Loading...</my-app> </body> </html>
HMTL中三個部分須要說明一下:
加載咱們須要的 javascript庫, 附錄中會有詳細的介紹
配置了 System 並讓他import 引入 main 文件
添加 my-app 這個HTML元素,這裏纔是加載咱們Angular實例的地方!
咱們須要一些東西來加載應用的模塊,這裏咱們使用 SystemJs。 這裏有不少選擇,SystemJS不必定是最好的選擇,可是這個挺好用。
10.
只須要在終端中輸入
npm start
程序將會將Typescript編譯成 Javascript ,同事啓動一個 lite-server, 加載咱們編寫的index.html。 顯示 My First Angular 2 App.