利用VS Code开发你的第一个AngularJS 2应用程序


Posted in Javascript onDecember 15, 2017

前言

之前已经给大家介绍了Angular2开发环境搭建教程之VS Code,本文将详细介绍利用VS Code如何开发AngularJS2应用程序的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

运行环境:

1、Windows 10

2、Node 6.7.0

利用VS Code开发你的第一个AngularJS 2应用程序

3、npm 3.10.8

利用VS Code开发你的第一个AngularJS 2应用程序

4、TypeScript 2.0.3

利用VS Code开发你的第一个AngularJS 2应用程序

创建项目

1、创建文件夹:angular2-quickstart,启动VS Code,打开刚创建的文件夹:angular2-quickstart。

2、在根文件夹(angular2-quickstart)下,创建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"
 }
}

3、在根文件夹(angular2-quickstart)下,创建tsconfig.json文件:

{
 "compilerOptions": {
 "target": "es5",
 "module": "commonjs",
 "moduleResolution": "node",
 "sourceMap": true,
 "emitDecoratorMetadata": true,
 "experimentalDecorators": true,
 "removeComments": false,
 "noImplicitAny": false
 }
}

4、在根文件夹(angular2-quickstart)下,创建typings.json文件:

{
 "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"
 }
}

5、在根文件夹(angular2-quickstart)下,创建systemjs.config.js(JavaScript脚本)文件:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function(global) {
 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'
  }
 }
 });
})(this);

文件结构:

|_ angular2-quickstart
|_ app
| |_ app.component.ts
| |_ main.ts
|_ node_modules ...
|_ typings ...
|_ index.html
|_ package.json
|_ tsconfig.json
|_ typings.json

安装依赖包(最关键一步

使用 npm 命令来安装 package.json 中列出的依赖包。在命令行 cmd 窗口,输入:cd angular2-quickstart,进入angular2-quickstar文件夹下,输入下列命令:

npm install

利用VS Code开发你的第一个AngularJS 2应用程序

创建TypeScript应用程序

1、在VS Code中,在根文件夹(angular2-quickstart)下,创建app子文件夹。

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

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 { }

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

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

4、在子app文件夹下,创建TypeScript文件main.ts:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);

5、在根文件夹(angular2-quickstart)下,创建html文件index.html:

<html>
<head>
 <title>Angular QuickStart</title>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet" href="styles.css">
 <!-- 1. Load libraries -->
 <!-- Polyfill(s) for older browsers -->
 <script src="node_modules/core-js/client/shim.min.js"></script>
 <script src="node_modules/zone.js/dist/zone.js"></script>
 <script src="node_modules/reflect-metadata/Reflect.js"></script>
 <script src="node_modules/systemjs/dist/system.src.js"></script>
 <!-- 2. Configure SystemJS -->
 <script src="systemjs.config.js"></script>
 <script>
  System.import('app').catch(function(err) {
   console.error(err);
  });
 </script>
</head>
<!-- 3. Display the application -->
<body>
 <my-app>Loading...</my-app>
</body>
</html>

6、在根文件夹(angular2-quickstart)下,创建css文件styles.css:

/* 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;
}

配置应用程序

1、在VS Code中,在根文件夹(angular2-quickstart)下,创建.vscode子文件夹。

2、在.vscode子文件夹下,创建settings.json文件:

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

3、在.vscode子文件夹下,创建tasks.json文件:

{
 // 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"]
}

运行应用程序至此,配置完毕,按 Ctrl + Shift + B 编译,程序将会将Typescript编译成 Javascript ,同时启动一个 lite-server, 加载我们编写的index.html。 显示:我的第一个 Angular 2 应用程序

利用VS Code开发你的第一个AngularJS 2应用程序

利用VS Code开发你的第一个AngularJS 2应用程序

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

参考资料:

  • 快速起步
  • 打造AngularJs2.0开发环境
  • AngularJs 2 快速入门
  • AngularJS2.0起步
Javascript 相关文章推荐
JavaScipt基本教程之JavaScript语言的基础
Jan 16 Javascript
jQuery Div中加载其他页面的实现代码
Feb 27 Javascript
在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗
Jun 02 Javascript
JScript分割字符串示例代码
Sep 04 Javascript
元素未显示设置width/height时IE中使用currentStyle获取为auto
May 04 Javascript
javascript实现图片上传前台页面
Aug 18 Javascript
AngularJS入门教程之路由与多视图详解
Aug 19 Javascript
JavaScript 实现 Tab 点击切换实例代码
Mar 25 Javascript
angular2 ng2 @input和@output理解及示例
Oct 10 Javascript
在element-ui的el-tree组件中用render函数生成el-button的实例代码
Nov 05 Javascript
Vue的click事件防抖和节流处理详解
Nov 13 Javascript
vue 扩展现有组件的操作
Aug 14 Javascript
Angular2开发环境搭建教程之VS Code
Dec 15 #Javascript
JavaScript原生实现观察者模式的示例
Dec 15 #Javascript
基于javascript 显式转换与隐式转换(详解)
Dec 15 #Javascript
ReactNative中使用Redux架构总结
Dec 15 #Javascript
Angular中使用MathJax遇到的一些问题
Dec 15 #Javascript
vue实现验证码输入框组件
Dec 14 #Javascript
基于滚动条位置判断的简单实例
Dec 14 #Javascript
You might like
php数组函数序列 之shuffle()和array_rand() 随机函数使用介绍
2011/10/29 PHP
PHP使用strtotime计算两个给定日期之间天数的方法
2015/03/18 PHP
PHP实现的增强性mhash函数
2015/05/27 PHP
PHP读取目录树的实现方法分析
2019/03/22 PHP
关于jQuery参考实例2.0 用jQuery选择元素
2013/04/07 Javascript
关闭ie窗口清除Session的解决方法
2014/01/10 Javascript
jQuery实现提示密码强度的代码
2015/07/15 Javascript
Vue数据驱动模拟实现3
2017/01/11 Javascript
js实现下一页页码效果
2017/03/07 Javascript
文本溢出插件jquery.dotdotdot.js使用方法详解
2017/06/22 jQuery
Vue2.0 实现单选互斥的方法
2018/04/13 Javascript
微信小程序开发背景图显示功能
2018/08/08 Javascript
深入理解react-router 路由的实现原理
2018/09/26 Javascript
webpack4实现不同的导出类型
2019/04/09 Javascript
微信小程序实现跳转的几种方式总结(推荐)
2019/04/24 Javascript
js实现网页版贪吃蛇游戏
2020/02/22 Javascript
[12:29]《一刀刀一天》之DOTA全时刻19:蝙蝠骑士田伯光再度不举
2014/06/10 DOTA
利用Python操作消息队列RabbitMQ的方法教程
2017/07/19 Python
python爬虫之urllib3的使用示例
2018/07/09 Python
python 中如何获取列表的索引
2019/07/02 Python
Django ModelForm组件使用方法详解
2019/07/23 Python
10行Python代码计算汽车数量的实现方法
2019/10/23 Python
pygame实现打字游戏
2021/02/19 Python
python实现门限回归方式
2020/02/29 Python
python构造IP报文实例
2020/05/05 Python
英国豪华真皮和布艺沙发销售网站:Darlings of Chelsea
2018/01/05 全球购物
Gap英国官网:Gap UK
2018/07/18 全球购物
应届生法律顾问求职信
2013/11/19 职场文书
财务工作者先进事迹材料
2014/01/17 职场文书
制作部班长职位说明书
2014/02/26 职场文书
家长对学生的评语
2014/04/18 职场文书
村主任“四风”问题个人对照检查材料思想汇报
2014/10/02 职场文书
涉外离婚协议书怎么写
2014/11/20 职场文书
感谢信的格式
2015/01/21 职场文书
大学生党员个人总结
2015/02/13 职场文书
Pandas-DataFrame知识点汇总
2022/03/16 Python