もっく日記

書いておぼえるためのブログ

AngularJS2:5 MIN QUICKSTARTをやってみる

AngularJS2の勉強用に「5 MIN QUICKSTART」をやってみたのでその時のメモ

「AngularJS2:5 MIN QUICKSTART」 https://angular.io/docs/ts/latest/quickstart.html

ファイル構成

最終的にはこのようなファイル構成になります

angular2-quickstart
├── app
│  ├── main.ts
│  └── app.component.ts
│ 
├── index.html
│ 
├── tsconfig.json
├── typings.json
└── package.json

1 フォルダを作ります

mkdir angular2-quickstart
cd    angular2-quickstart

2 tsconfig.jsonをつくります

angular2-quickstart直下にtsconfig.jsonをつくります

//tsconfig.json
{
  "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 typings.jsonをつくります

angular2-quickstart直下にtypings.jsonをつくります

//typings.json
{
  "ambientDependencies": {
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2"
  }
}

4 package.jsonをつくります

angular2-quickstart直下にpackage.jsonをつくります

//package.json
{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
    "postinstall": "npm run typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",
    "typings" : "typings"
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.6",
    "systemjs": "0.19.20",
    "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.14"
  },
  "devDependencies": {
    "concurrently": "^1.0.0",
    "lite-server": "^2.0.1",
    "typescript": "^1.7.5",
    "typings":"^0.6.8"
  }
}

5 ライブラリをインストール

ターミナルで

npm install

を実行すると、node_modulestypingsフォルダにライブラリがインストールされます。

6 app.component.tsmain.tsファイルをつくります

appフォルダをつくり、その直下にapp.component.tsmain.tsファイルをつくります

//app.component.ts
import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }
//main.ts
import {bootstrap}    from 'angular2/platform/browser'
import {AppComponent} from './app.component'

bootstrap(AppComponent);

7 index.htmlをつくります

angular2-quickstart直下にindex.htmlをつくる。

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

    <!-- 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>

8 実行

ターミナルで

npm start

を実行

「My First Angular 2 App」の文字が表示されれば完了です。

もし loading のままになったら

index.htmlSystem.import('app/main')System.import('app/main.js')拡張子つけると表示されるかもしれません。 System.configでJSを設定してるみたいなんですが。

こちらのサイトが勉強になりました