利用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 相关文章推荐
jQuery ajax BUG:object doesn't support this property or method
Jul 06 Javascript
jQuery验证Checkbox是否选中的代码 推荐
Sep 04 Javascript
JavaScript定义类的几种方式总结
Jan 06 Javascript
动态创建script在IE中缓存js文件时导致编码的解决方法
May 04 Javascript
什么是MEAN?JavaScript编程中的MEAN是什么意思?
Dec 18 Javascript
jQuery中prevUntil()方法用法实例
Jan 08 Javascript
jQuery实现元素拖拽并cookie保存顺序的方法
Feb 20 Javascript
卸载安装Node.js与npm过程详解
Aug 15 Javascript
JS基于正则截取替换特定字符之间字符串操作示例
Feb 03 Javascript
微信小程序全局变量改变监听的实现方法
Jul 15 Javascript
jquery实现购物车基本功能
Oct 25 jQuery
js实现幻灯片轮播图
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
Codeigniter操作数据库表的优化写法总结
2014/06/12 PHP
php可扩展的验证类实例(可对邮件、手机号、URL等验证)
2015/07/09 PHP
PHP下载远程图片并保存到本地方法总结
2016/01/22 PHP
PHP从二维数组得到N层分类树的实现代码
2016/10/11 PHP
html a标签-超链接中confirm方法使用介绍
2013/01/04 Javascript
判断日期是否能跨月查询的js代码
2014/07/25 Javascript
JavaScript function 的 length 属性使用介绍
2014/09/15 Javascript
JS小游戏之象棋暗棋源码详解
2014/09/25 Javascript
IE中鼠标经过option触发mouseout的解决方法
2015/01/29 Javascript
jQuery EasyUI实现右键菜单变灰不可用效果
2015/09/24 Javascript
js 弹出对话框(遮罩)透明,可拖动的简单实例
2016/07/11 Javascript
AngularJS基础 ng-keydown 指令简单示例
2016/08/02 Javascript
Javascript实现的StopWatch功能示例
2017/06/13 Javascript
jQuery ajax动态生成table功能示例
2017/06/14 jQuery
微信小程序实现图片上传功能实例(前端+PHP后端)
2018/01/10 Javascript
使用node.JS中的url模块解析URL信息
2020/02/06 Javascript
微信小程序报错: thirdScriptError的错误问题
2020/06/19 Javascript
[53:10]完美世界DOTA2联赛决赛日 FTD vs GXR 第二场 11.08
2020/11/11 DOTA
Python抓取百度查询结果的方法
2015/07/08 Python
Python网络爬虫实例讲解
2016/04/28 Python
Python数据操作方法封装类实例
2017/06/23 Python
Python程序运行原理图文解析
2018/02/10 Python
python读取ini配置的类封装代码实例
2020/01/08 Python
python numpy库np.percentile用法说明
2020/06/08 Python
Python局部变量与全局变量区别原理解析
2020/07/14 Python
非洲NO.1网上商店:Jumia肯尼亚
2016/08/18 全球购物
澳大利亚购买太阳镜和眼镜网站:Glamoureyes
2020/09/22 全球购物
毕业生医学检验求职信
2013/10/16 职场文书
护士自荐信
2013/10/25 职场文书
护理个人求职信范文
2014/01/08 职场文书
展会邀请函范文
2014/01/26 职场文书
2014年道德讲堂实施方案
2014/03/05 职场文书
教师自我剖析材料(群众路线)
2014/09/29 职场文书
给领导敬酒词
2015/08/12 职场文书
2016年全国助残日活动总结
2016/04/01 职场文书
DQL数据查询语句使用示例
2022/12/24 MySQL