Visual Studio Code——Angular2 Hello World 之 2.0

  最近看到一篇用Visual Studio Code開發Angular2的文章,也是一篇入門教程,地址爲:使用Visual Studio Code開發Angular 2專案。這裏循序漸進的作了一遍,感受很方便,而且沒有遇到樓主的一些問題,應該是安裝環境有些不一樣。這裏只爲記錄一下。再次感謝!css

  1、隨便新建一個目錄,這裏爲:F:\Visual Studio Code\angular2_1,並用Visual Studio Codehtml

  2、依次新建以下四個文件,參考https://angular.cn/docs/ts/latest/quickstart.htmlnode

    package.json 用來標記出本項目所需的 npm 依賴包。web

         tsconfig.json 定義了 TypeScript 編譯器如何從項目源文件生成 JavaScript 代碼。typescript

     typings.json 爲那些 TypeScript 編譯器沒法識別的庫提供了別的定義文件。npm

     systemjs.config.js 爲模塊加載器提供了該到哪裏查找應用模塊的信息,並註冊了全部必備的依賴包。 它還包括文檔中後面的例子須要用到的包。json

 1 {
 2   "name": "angular-quickstart",
 3   "version": "1.0.0",
 4   "scripts": {
 5     "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
 6     "lite": "lite-server",
 7     "postinstall": "typings install",
 8     "tsc": "tsc",
 9     "tsc:w": "tsc -w",
10     "typings": "typings"
11   },
12   "license": "ISC",
13   "dependencies": {
14     "@angular/common": "~2.0.1",
15     "@angular/compiler": "~2.0.1",
16     "@angular/core": "~2.0.1",
17     "@angular/forms": "~2.0.1",
18     "@angular/http": "~2.0.1",
19     "@angular/platform-browser": "~2.0.1",
20     "@angular/platform-browser-dynamic": "~2.0.1",
21     "@angular/router": "~3.0.1",
22     "@angular/upgrade": "~2.0.1",
23     "angular-in-memory-web-api": "~0.1.1",
24     "bootstrap": "^3.3.7",
25     "core-js": "^2.4.1",
26     "reflect-metadata": "^0.1.8",
27     "rxjs": "5.0.0-beta.12",
28     "systemjs": "0.19.39",
29     "zone.js": "^0.6.25"
30   },
31   "devDependencies": {
32     "concurrently": "^3.0.0",
33     "lite-server": "^2.2.2",
34     "typescript": "^2.0.3",
35     "typings":"^1.4.0"
36   }
37 }
package.json
 1 {
 2   "compilerOptions": {
 3     "target": "es5",
 4     "module": "commonjs",
 5     "moduleResolution": "node",
 6     "sourceMap": true,
 7     "emitDecoratorMetadata": true,
 8     "experimentalDecorators": true,
 9     "removeComments": false,
10     "noImplicitAny": false
11   }
12 }
tsconfig.json
1 {
2   "globalDependencies": {
3     "core-js": "registry:dt/core-js#0.0.0+20160725163759",
4     "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
5     "node": "registry:dt/node#6.0.0+20160909174046"
6   }
7 }
typings.json
 1 /**
 2  * System configuration for Angular samples
 3  * Adjust as necessary for your application needs.
 4  */
 5 (function (global) {
 6   System.config({
 7     paths: {
 8       // paths serve as alias
 9       'npm:': 'node_modules/'
10     },
11     // map tells the System loader where to look for things
12     map: {
13       // our app is within the app folder
14       app: 'app',
15       // angular bundles
16       '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
17       '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
18       '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
19       '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
20       '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
21       '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
22       '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
23       '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
24       // other libraries
25       'rxjs':                      'npm:rxjs',
26       'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
27     },
28     // packages tells the System loader how to load when no filename and/or no extension
29     packages: {
30       app: {
31         main: './main.js',
32         defaultExtension: 'js'
33       },
34       rxjs: {
35         defaultExtension: 'js'
36       },
37       'angular-in-memory-web-api': {
38         main: './index.js',
39         defaultExtension: 'js'
40       }
41     }
42   });
43 })(this);
systemjs.config.js

  3、按着「shift」鍵,右鍵項目文件夾,選擇「在此處打開命令窗口(W)」,鍵入DOS命令窗口,鍵入」npm install「,截圖以下:bootstrap

  下載了好多文件,依賴的類庫可真多,以後的文件夾目錄以下:api

  4、在根目錄下新建app文件夾,並依次建立下面三個文件app.module.ts、app.component.ts、main.ts;並在根目錄下新建html頁面——index.htmlangular2

1 import { NgModule }      from '@angular/core';
2 import { BrowserModule } from '@angular/platform-browser';
3 import { AppComponent }   from './app.component';
4 @NgModule({
5   imports:      [ BrowserModule ],
6   declarations: [ AppComponent ],
7   bootstrap:    [ AppComponent ]
8 })
9 export class AppModule { }
app.module.ts
1 import { Component } from '@angular/core';
2 @Component({
3   selector: 'my-app',
4   template: '<h1>My First Angular App</h1>'
5 })
6 export class AppComponent { }
app.component.ts
1 import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
2 import { AppModule } from './app.module';
3 const platform = platformBrowserDynamic();
4 platform.bootstrapModule(AppModule);
main.ts
 1 <html>
 2   <head>
 3     <title>Angular QuickStart</title>
 4     <meta charset="UTF-8">
 5     <meta name="viewport" content="width=device-width, initial-scale=1">
 6     <link rel="stylesheet" href="styles.css">
 7     <!-- 1. Load libraries -->
 8      <!-- Polyfill(s) for older browsers -->
 9     <script src="node_modules/core-js/client/shim.min.js"></script>
10     <script src="node_modules/zone.js/dist/zone.js"></script>
11     <script src="node_modules/reflect-metadata/Reflect.js"></script>
12     <script src="node_modules/systemjs/dist/system.src.js"></script>
13     <!-- 2. Configure SystemJS -->
14     <script src="systemjs.config.js"></script>
15     <script>
16       System.import('app').catch(function(err){ console.error(err); });
17     </script>
18   </head>
19   <!-- 3. Display the application -->
20   <body>
21     <my-app>Loading...</my-app>
22   </body>
23 </html>
index.html

  5、按照以下截圖,設定啓動做業:

  設置成功以後,在啓動文件launch.json中作如上圖修改:${workspaceRoot}/node_modules/lite-server/bin/lite-server

  6、在菜單欄中,點擊」查看「/」命令面板「或者快捷鍵」cltr+shift+p「,鍵入」configure「,選擇」任務:配置任務運行程序「,以後選擇」TypeScript-tsconfig.json 建立TypeScript項目「,以下圖:

  按照教程博客所說,要task.json作如上圖修改,不然下面調試時若是出現錯誤沒有任何提示。

  7、以上已經完成了全部的工做,開始調試。按下」ctrl+shift+b「,執行編譯,會將ts文件編譯爲js文件,以下圖:

  若是編譯成功(如上圖),就能夠按」F5「了,以下圖:

  可是若是編譯不成功,應該是沒有安裝tsc,使用命令」npm install -g typescript「安裝一下,以後再編譯就沒問題了,以下圖:

  這裏徹底是按着上面的教程來的,有點很差意思,只做爲練習記錄,再次感謝!

相關文章
相關標籤/搜索