一步接一步开始angular2

配置webstrom开启Javascript所有新特性支持

File > Setting > Language&Frameworks > javascript > javascript language version "jsx-harmony"  

配置typescript

File > Setting > Language&Frameworks > typescript > enable complier 并且点选use tsconfig.json  

添加依赖的node模块,创建packjson.json文件

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.0",
    "systemjs": "0.19.6",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.0",
    "zone.js": "0.5.10"
  },
  "devDependencies": {
    "concurrently": "^1.0.0",
    "lite-server": "^1.3.1",
    "typescript": "^1.7.3"
  }
}

为typescript编译器创建配置tsconfig.json文件

 {
    "compilerOptions": {
      "target": "ES5",
      "module": "system",
      "moduleResolution": "node",
      "sourceMap": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "removeComments": false,
      "noImplicitAny": false
    },
    "exclude": [
      "node_modules"
    ]
  }

创建应用程序源码子文件夹

$ mkdir app
$ cd app

创建组件文件app/app.component.ts

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }  

添加引导app/boot.ts

import {bootstrap}    from 'angular2/platform/browser'  
import {AppComponent} from './app.component'

bootstrap(AppComponent);  

添加index.html

$ cd ..

创建index.html

<html>

  <head>
    <title>Angular 2 QuickStart</title>

    <!-- 1. Load libraries -->
    <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/boot')
            .then(null, console.error.bind(console));
    </script>

  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>

</html>  

编译并运行

$ Compile and run!

打开浏览器输入127.0.0.1:3000

为什么需要boot.js

因为Angular “core” 兼容全平台,的确,很多Angular应用程序都运行在浏览器端,很多时候我们从库中回调bootstrap 方法,使它可以在浏览器上更好的运行,但是它也有可能在不同环境中载入不同的组件,我们可能会在移动设备中载入Apache Cordova或NativeScript.我们希望应用程序有更好的性能,更适合seo优化,可以在不同的环境中载入不同的库

你的欣赏是我最大的动力

Yanxiong Huang

My name is Yanxiong Huang. graduated from Nanyang middle school.Love Linux,familiar with Node.js,Docker,Serverless... and more Web technology.Contact Me:QQ 31356617;Email:huangaynxiong2013@gmail.com

guangzhou,china http://www.myfreax.com

乐在分享