Angular 2 Hello World

8352 ワード

使用するAngular 2.0 Beta版!
Angular 2は十分な金持ちの2世代で、そのお父さんはマイクロソフトで、彼のお母さんはGoogleで、どうしてマイクロソフトはお父さんで、Googleはお母さんですか?Angular 2はtypescriptによって開発されているので(男の子と女の子を産むのはお父さんが決める!~)
  • インストールに必要な依存パッケージ:
  • 適当に場所を探してフォルダangular 2-helloworldを新築して、cmdを開けて、cdはangular 2-helloworldフォルダディレクトリに入ります
    npm init

    一路車に戻って暴れて、この時package.jsonファイルを生成します(もしあなたがまだnode環境を詰めていないならば、それではまた毛糸を見て、急いで先にインストールしてから帰ってきます!!)
    npm install angular2 systemjs --save
    npm install -g typescript http-server

    Systemjs:この品物はAMD/ES 6モジュールローダです.
    typescript http-serverをグローバルにインストールして、後で使用するようにしてください.そして現在のプロジェクトにもtypescriptとhttp-serverをインストールします
    npm install typescript http-server --save-dev

    (プラス--saveインストールのパッケージはpackage.jsonのdependenciesの下に追加されます.プラス--save-devインストールのパッケージはdevDependenciesの下に自動的に追加されます.知っていますか?じゃ、余計なことを言って、ほほほ~)
  • hello world~
  • を書き始めました
    main.tsファイルを新規作成します(ファイル接尾辞は.tsであることを覚えています)
    import {Component} from 'angular2/core'
    import {bootstrap} from 'angular2/platform/browser'
    
    @Component({
      selector: 'my-app',
      template: '

    Hello {{ str }}!!!

    ' }) export class AppComponent{ public str = 'World'; }; bootstrap(AppComponent);

    新しいindex.htmlファイルを作成します
    DOCTYPE html>
    <html>
    <head>
      <title>Angular 2 Helloworldtitle>
    
      
      <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>
    
      
      <script>
        System.import('main.js').then(null, console.error.bind(console));
      script>
    head>
    <body>
      
      <my-app>Loading...my-app>
    body>
    html>
  • hello worldプログラム
  • を実行
    cmdに入力
    tsc --init

    tsconfig.jsonを生成します(これは.tsファイルをjsファイルにコンパイルする構成で、他のパラメータもあります.自分で検討できます).tsconfig.jsonの内容は次のとおりです.
    {
      "compilerOptions": {
        "target": "ES5",
        "module": "system",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false
      },
      "exclude": [
        "node_modules"
      ]
    }

    コンパイルを開始.tsは.js、入力
    tsc --watch

    main.jsと対応するmain.js.mapが生成されます
    新しいcmdウィンドウを開く うんてん
    http-server

    ブラウザに127.0.0.1:8080と入力 大きくて黒いのが見えます
    Hello World!!!
     
    転載先:https://www.cnblogs.com/wning/p/5063782.html