2应用程序_AngularJS_脚本之家

前言

www.js8331.com,  近年来看到一篇用Visual Studio
Code开荒Angular2的小说,也是一篇入门教程,地址为:使用Visual Studio
Code開發Angular
2專案。这里绳趋尺步的做了三次,以为很有利,而且未有遭遇楼主的一些题目,应该是设置情状有个别区别。这里只为记录一下。再度多谢!

以前早就给我们介绍了Angular2开辟条件搭建教程之VS
Code,本文将详细介绍利用VS
Code怎么样开拓AngularJS2应用程序的相干内容,分享出去供我们参谋学习,上边话非常的少说了,来一块看看详细的牵线吧。

  一、随意新建四个索引,这里为:F:Visual
Studio Code2应用程序_AngularJS_脚本之家。angular2_1,并用Visual Studio Code

运作景况:

  二、依次新建如下多个文本,参照他事他说加以侦查

1、Windows 10

    package.json
用来标识出本项目所需的 npm 信赖包。

2、Node 6.7.0

         tsconfig.json 定义了 TypeScript 编写翻译器怎么着从连串源文件生成
JavaScript 代码。

3、npm 3.10.8

*     typings.json 为这个 TypeScript
编写翻译器不大概甄其余库提供了其他定义文件。*

4、TypeScript 2.0.3

*     systemjs.config.js
为模块加载器提供了该到哪里搜索应用模块的新闻,并注册了富有供给的注重包。
它还包罗文书档案中前面包车型地铁例证供给采纳的包。*

创设项目

www.js8331.com 1www.js8331.com 2

1、成立文件夹:angular2-quickstart,运营VS
Code,展开刚创设的文书夹:angular2-quickstart。

 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 }

2、在根文件夹下,创立package.json文件:

package.json

{ "name": "angular-quickstart", "version": "1.0.0", "scripts": { "start": "tsc && concurrently "tsc -w" "lite-server" ", "lite": "lite-server", "postinstall": "typings install", "tsc": "tsc", "tsc:w": "tsc -w", "typings": "typings" }, "license": "ISC", "dependencies": { "@angular/common": "~2.0.2", "@angular/compiler": "~2.0.2", "@angular/core": "~2.0.2", "@angular/forms": "~2.0.2", "@angular/http": "~2.0.2", "@angular/platform-browser": "~2.0.2", "@angular/platform-browser-dynamic": "~2.0.2", "@angular/router": "~3.0.2", "@angular/upgrade": "~2.0.2", "angular-in-memory-web-api": "~0.1.5", "bootstrap": "^3.3.7", "core-js": "^2.4.1", "reflect-metadata": "^0.1.8", "rxjs": "5.0.0-beta.12", "systemjs": "0.19.39", "zone.js": "^0.6.25" }, "devDependencies": { "concurrently": "^3.1.0", "lite-server": "^2.2.2", "typescript": "^2.0.3", "typings": "^1.4.0" }}

www.js8331.com 3www.js8331.com 4

3、在根文件夹下,创立tsconfig.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 }
{ "compilerOptions": { "target": "es5", "module": "commonjs", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false }}

tsconfig.json

4、在根文件夹下,成立typings.json文件:

www.js8331.com 5www.js8331.com 6

{ "globalDependencies": { "core-js": "registry:dt/core-js#0.0.0+20160725163759", "jasmine": "registry:dt/jasmine#2.2.0+20160621224255", "node": "registry:dt/node#6.0.0+20160909174046" }}
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 }

5、在根文件夹下,成立systemjs.config.js文件:

typings.json

/** * System configuration for Angular samples * Adjust as necessary for your application needs. */ { System.config({ paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { // our app is within the app folder app: 'app', // angular bundles '@angular/core': 'npm:@angular/core/bundles/core.umd.js', '@angular/common': 'npm:@angular/common/bundles/common.umd.js', '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', '@angular/http': 'npm:@angular/http/bundles/http.umd.js', '@angular/router': 'npm:@angular/router/bundles/router.umd.js', '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', // other libraries 'rxjs': 'npm:rxjs', 'angular-in-memory-web-api': 'npm:angular-in-memory-web-api', }, // packages tells the System loader how to load when no filename and/or no extension packages: { app: { main: './main.js', defaultExtension: 'js' }, rxjs: { defaultExtension: 'js' }, 'angular-in-memory-web-api': { main: './index.js', defaultExtension: 'js' } } });});

www.js8331.com 7www.js8331.com 8

文件结构:

 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);
|_ angular2-quickstart|_ app| |_ app.component.ts| |_ main.ts|_ node_modules ...|_ typings ...|_ index.html|_ package.json|_ tsconfig.json|_ typings.json

systemjs.config.js

安装信任包

  三、按着“shift”键,右键项目文件夹,选取“在那间展开命令窗口(W)”,键入DOS命令窗口,键入”npm
install“,截图如下:

动用 npm 命令来安装 package.json 中列出的信任包。在指令行 cmd
窗口,输入:cd
angular2-quickstart,步向angular2-quickstar文件夹下,输入下列命令:

www.js8331.com 9

npm install

  下载了累累文件,依赖的类库可真多,之后的文书夹目录如下:

成立TypeScript应用程序

www.js8331.com 10

1、在VS Code中,在根文件夹下,创立app子文件夹。

  四、在根目录下新建app文件夹,并逐一创制下边多个公文app.module.ts、app.component.ts、main.ts;并在根目录下新建html页面——index.html

2、在子app文件夹下,创制TypeScript文件app.module.ts:

www.js8331.com 11www.js8331.com 12

import { NgModule } from '@angular/core';import { BrowserModule } from '@angular/platform-browser';import { AppComponent } from './app.component';@NgModule({ imports: [ BrowserModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ]})export class AppModule { }
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 { }

3、在子app文件夹下,创立TypeScript文件app.component.ts:

app.module.ts

import { Component } from '@angular/core';@Component({ selector: 'my-app', template: '我的第一个 AngularJS 2 应用程序'})export class AppComponent { }

www.js8331.com 13www.js8331.com 14

4、在子app文件夹下,创设TypeScript文件main.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 { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';import { AppModule } from './app.module';const platform = platformBrowserDynamic();platform.bootstrapModule;

app.component.ts

5、在根文件夹下,创造html文件index.html:

www.js8331.com 15www.js8331.com 16

 Angular QuickStart           System.import.catch { console.error;  Loading...
1 import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
2 import { AppModule } from './app.module';
3 const platform = platformBrowserDynamic();
4 platform.bootstrapModule(AppModule);

6、在根文件夹下,创建css文件styles.css:

main.ts

/* Master Styles */h1 { color: #369; font-family: Arial, Helvetica, sans-serif; font-size: 250%;}h2,h3 { color: #444; font-family: Arial, Helvetica, sans-serif; font-weight: lighter;}body { margin: 2em;}

www.js8331.com 17www.js8331.com 18

安插应用程序

 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>

1、在VS Code中,在根文件夹下,创建.vscode子文件夹。

index.html

2、在.vscode子文件夹下,制造settings.json文件:

  五、依据如下截图,设定运行作业:

// 将设置放入此文件中以覆盖默认值和用户设置。{ "typescript.tsdk": "node_modules/typescript/lib", // ts 项目, 隐藏 .js 和 .js.map 文件 "files.exclude": { "node_modules": true, "**/*.js": { "when": "$.ts" }, "**/*.js.map": true }}

www.js8331.com 19

3、在.vscode子文件夹下,成立tasks.json文件:

www.js8331.com 20

{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "0.1.0", "command": "cmd", "isShellCommand": true, "showOutput": "always", "args": ["/C npm start"]}

  设置成功未来,在运行文件launch.json中做如上海体育场合改过:${workspaceRoot}/node_modules/lite-server/bin/lite-server

运营应用程序至此,配置完毕,按 Ctrl + Shift + B
编写翻译,程序将会将Typescript编写翻译成 Javascript ,同期开动一个 lite-server,
加载大家编辑的index.html。 展现:小编的率先个 Angular 2 应用程序

  六、在菜单栏中,点击”查看“/”命令面板“或许急忙键”cltr+shift+p“,键入”configure“,选取”任务:配置任务运路程序“,之后选取”TypeScript-tsconfig.json
创设TypeScript项目“,如下图:

总结

www.js8331.com 21

上述正是那篇文章的全体内容了,希望本文的内容对我们的学习只怕办事有着一定的参照学习价值,借使反常我们可以留言调换,多谢我们对台本之家的帮忙。

www.js8331.com 22

参考资料:

www.js8331.com 23

高效起步 构建AngularJs2.0开采条件 AngularJs 2 快捷入门 AngularJS2.0开发银行

  依据教程博客所说,要task.json做如上海体育场合改进,不然下面调节和测量试验时即便现身错误未有别的提示。

  七、以寒小品方完毕了全体的做事,早先调和。按下”ctrl+shift+b“,推行编译,会将ts文件编写翻译为js文件,如下图:

www.js8331.com 24

  假诺编写翻译成功(如上海体育场所),就足以按”F5“了,如下图:

www.js8331.com 25

  可是只要编写翻译不成功,应该是从未有过安装tsc,使用命令”npm
install -g typescript“安装一下,之后再编译就没难题了,如下图:

www.js8331.com 26

  这里完全部是按着上边的课程来的,有一些不好意思,只看做练兵记录,再次谢谢!

发表评论

电子邮件地址不会被公开。 必填项已用*标注