利用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 相关文章推荐
仅IE9/10同时支持script元素的onload和onreadystatechange事件分析
Apr 27 Javascript
js模拟滚动条(横向竖向)
Feb 22 Javascript
文本框(input)获取焦点(onfocus)时样式改变的示例代码
Jan 10 Javascript
Struts2+jquery.form.js实现图片与文件上传的方法
May 05 Javascript
jQuery自定义图片缩放拖拽插件imageQ实现方法(附demo源码下载)
May 27 Javascript
ReactNative页面跳转实例代码
Sep 27 Javascript
jQuery插件FusionCharts实现的2D面积图效果示例【附demo源码下载】
Mar 06 Javascript
JS排序算法之希尔排序与快速排序实现方法
Dec 12 Javascript
Vue中的slot使用插槽分发内容的方法
Mar 01 Javascript
Vue.js图片预览插件使用详解
Aug 27 Javascript
JavaScript格式化json和xml的方法示例
Jan 22 Javascript
PHP读取远程txt文档到数组并实现遍历
Aug 25 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中使用sockets:从新闻组中获取文章
2006/10/09 PHP
php 攻击方法之谈php+mysql注射语句构造
2009/10/30 PHP
PHP数据流应用的一个简单实例
2012/09/14 PHP
php中get_defined_constants函数用法实例分析
2015/05/12 PHP
PHP模拟http请求的方法详解
2016/11/09 PHP
PHP生成唯一ID之SnowFlake算法
2016/12/17 PHP
PHP开发的微信现金红包功能示例
2017/06/29 PHP
tp5(thinkPHP5框架)captcha验证码配置及验证操作示例
2019/05/28 PHP
javascript(jquery)利用函数修改全局变量的代码
2009/11/02 Javascript
JavaScript中的几个关键概念的理解-原型链的构建
2011/05/12 Javascript
js通过元素class名字获取元素集合的具体实现
2014/01/06 Javascript
jQuery对象初始化的传参方式
2015/02/26 Javascript
Fullpage.js固定导航栏-实现定位导航栏
2016/03/17 Javascript
Bootstrap每天必学之按钮(Button)插件
2016/04/25 Javascript
Vue全局loading及错误提示的思路与实现
2019/08/09 Javascript
ES6之Proxy的get方法详解
2019/10/11 Javascript
深入分析JavaScript 事件循环(Event Loop)
2020/06/19 Javascript
vue中watch和computed的区别与使用方法
2020/08/23 Javascript
解决Vue项目中tff报错的问题
2020/10/21 Javascript
ssh批量登录并执行命令的python实现代码
2012/05/25 Python
Python+OpenCV图片局部区域像素值处理详解
2019/01/23 Python
浅谈python3.6的tkinter运行问题
2019/02/22 Python
Python Django给admin添加Action的方法实例详解
2019/04/29 Python
Python3 执行系统命令并获取实时回显功能
2019/07/09 Python
GDAL 矢量属性数据修改方式(python)
2020/03/10 Python
马来西亚在线时尚女装商店:KEI MAG
2017/09/28 全球购物
奥地利网上现代灯具和灯饰店:Lampenwelt.at
2018/01/29 全球购物
Sahajan美国:阿育吠陀护肤品牌
2021/01/09 全球购物
俄罗斯奢侈品牌衣服、鞋子和配饰的在线商店:INTERMODA
2020/07/17 全球购物
优秀共产党员先进事迹材料
2014/05/06 职场文书
老兵退伍标语
2014/10/07 职场文书
应届毕业生求职简历自我评价
2015/03/02 职场文书
保护环境的宣传语
2015/07/13 职场文书
Python中json.dumps()函数的使用解析
2021/05/17 Python
Java 超详细讲解十大排序算法面试无忧
2022/04/08 Java/Android
手把手带你彻底卸载MySQL数据库
2022/06/14 MySQL