angularjs 2.0 快速开始
前言
angularjs2.0 如果发布,公司的项目会基于2.0开发,在1.0的时候就踩了好多坑,趁这2.0还没正式发布,赶紧踩下坑。 这篇文章是参考angularjs2.0 官方文档写的,开发环境需要机器上有 nodejs环境,另外2.0 是基于typescript 开发,微软开发一种语言 ,附上中文手册链接 大家可以自行研究下..
开发环境的搭建
1.创建项目文件夹
由于我是在window 上开发,机器上装了Git Bash 所以我直接用命令来构建
mkdir angular2-quickstart
cd angular2-quickstart
2.添加一个 tsconfig.json 文件
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
这个文件是 typescript编译指南文件,也就是说这事 ts 的编译配置文件,关于更多 Typescript Configuration
2.添加一个 typings.json 文件
{
"ambientDependencies": {
"es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
"jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd"
}
}
一些js库扩展了JavaScript的特性和语法,但是TypeScript编译器并不识别,因此需要在typings.json文件中配置TypeScript类型定义文件(文件名后缀为.d.ts)
2.添加一个 package.json 文件
{
"name": "angular2-quickstart",
"version": "1.0.0",
"scripts": {
"start": "concurrently \"npm run tsc:w\" \"npm run lite\" ",
"tsc": "tsc",
"tsc:w": "tsc -w",
"lite": "lite-server",
"typings": "typings",
"postinstall": "typings install"
},
"license": "ISC",
"dependencies": {
"angular2": "2.0.0-beta.13",
"systemjs": "0.19.25",
"es6-shim": "^0.35.0",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.2",
"zone.js": "0.6.6"
},
"devDependencies": {
"concurrently": "^2.0.0",
"lite-server": "^2.1.0",
"typescript": "^1.8.9",
"typings":"^0.7.11"
}
}
angularjs 用 npm 安装所有的依赖包,我们用 npm install 来安装 。安装过程可能会出错,建议翻墙,如果npm出现 关于 typings 的错误,可能是你没有安装,这时候我们可以用命令npm install typings --global 去安装
package.json 文件中定义了一些脚本
npm start在观察模式下 ,运行编译和启动服务npm run tsc运行 typescript 编译器npm run tsc:w在观察模式下运行typescript编译器(监控 后缀.ts 文件的变化,文件一旦变化就会重新编译)npm run lite运行 lite-server 一个轻量级的静态文件服务器,用于支撑angular 的路由npm run typings运行 typings toolnpm run postinstallnpm 安装成功后自动调用 ,这个脚本安装 定义在 typings.json 文件当中
第一个angular组件( Angular2.0 Hello World)
创建一个app文件夹,并添加 app.component.ts 文件
import {Component} from 'angular2/core'; //导入一个模块
//组件是一个装饰功能,它需要一个元数据对象.元数据告诉angular如何创建和使用这个组件。
//通过用前缀@调用:
@Component({
selector:'my-app', //值为一个css选择器,组件元素的名称为my-app,Angular会为HTML中的my-app元素创建并显示AppComponent实例
template:'<h1>Angular 2 hello wold</h1>' //指定对应的模板,以及数据绑定
})
export class AppComponent { } //导出 AppComponent
每个angular的应用都有至少一根组件,通常叫AppComponent. 组件是angular应用程序的基本构建块。
创建main.ts 文件
import {bootstrap} from 'angular2/platform/browser'; //导入bootstrap 模块,用来启动angular
import {AppComponent} from './app.component'; //导入我们应用的根组件
bootstrap(AppComponent); //传入我们的根组件启动angular
创建index.html 文件
<html>
<head>
<title>Angular 2 hello world</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 1. Load libraries (加载一些公用的js) -->
<!-- IE required polyfills, in this exact order -->
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/systemjs/dist/system-polyfills.js"></script>
<script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<!-- 2. Configure SystemJS (配置系统js) -->
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app/main')
.then(null, console.error.bind(console));
</script>
</head>
<!-- 3. Display the application (显示我们应用程序) -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
启动
运行我们的命令npm start ,浏览器会自动帮我们打开 地址localhost:3000
我们就能看到 我们的程序了,是不是很激动呢。
结束语
angular2.0 相比1.0 简单了好多,使我们入门更加容易,大家赶快来试试吧,有哪里走不通的,欢迎留言,我及时解答,源码没上传github,如需源码 ,请@我。
angularjs 2.0 快速开始的更多相关文章
- angularjs 2.0 快速案例(1)
前言 上一节我们已经把环境给搭建起来了,现在我们通过一个快速案例把angular 2.0 初步了解一下,后续我们会深入每一个细节,这个案例主要是一个[英雄(Hero)]列表的展示,创建,编辑.这个案例 ...
- 转 AngularJS 2.0将面向移动应用并放弃旧浏览器
AngularJS团队表示“AngularJS 2.0是移动应用的框架”.该框架将继续支持桌面,但其主要关注点变成了移动领域.它的目标还包括通过转译器支持EcmaScript 6(因为浏览器还不支持E ...
- 八爪鱼招标网的百度权重升为2了,独立IP也从0快速发展为1000
自八爪鱼招标网上线以来,本着以客户一切利益为出发点,坚持提供国内首个免费招标信息平台为目标,经过各位同事不断地努力,不断收集客户各种各样的招标.采购实际需求,与政府.事业单位及中小型企业一对一的沟 ...
- Angular2.0快速开始
参考资料: Angular2.0快速开始 AngularJS2 教程
- 聊天系统Demo,增加文件传送功能(附源码)-- ESFramework 4.0 快速上手(14)
本文我们将介绍在ESFramework 4.0 快速上手(08) -- 入门Demo,一个简单的IM系统(附源码)的基础上,增加文件传送的功能.如果不了解如何使用ESFramework提供的文件传送功 ...
- 可靠通信的保障 —— 使用ACK机制发送自定义信息——ESFramework 通信框架4.0 快速上手(12)
使用ESPlus.Application.CustomizeInfo.Passive.ICustomizeInfoOutter接口的Send方法,我们已经可以给服务端或其它在线客户端发送自定义信息了, ...
- 聊天系统Demo,增加Silverlight客户端(附源码)-- ESFramework 4.0 快速上手(09)
在ESFramework 4.0 快速上手 -- 入门Demo,一个简单的IM系统(附源码)一文中,我们介绍了使用ESFramework的Rapid引擎开发的winform聊天程序,本文我们将在之前d ...
- ESFramework 4.0 快速上手(06) -- Rapid引擎(续)
<ESFramework 4.0 快速上手>系列介绍的都是如何使用Rapid引擎(快速引擎) -- RapidServerEngine 和 RapidPassiveEngine.其实,大家 ...
- 离线消息如何实现?-- ESFramework 4.0 快速上手(02)
在ESFramework 4.0 快速上手一文中,主要介绍了如何使用ESPlus.Rapid命名空间中的引擎来快速地构建基于TCP的网络通信系统,即使是使用ESPlus.Rapid来进行ESFrame ...
随机推荐
- Java_新浪微博SDK_jar包下载
新浪微博开放平台API_jar包下载地址:jar包(猛戳) --by HsuChan
- iOS 真机测试时报错:Provisioning profile "iOS Team Provisioning Profile: XXX” doesn't include the currently selected device “XXX”.
这几天因工作需要,去给客户演示iOS项目打包的过程.之前演示都是顺利的,但后来客户自己操作时打电话说遇到了问题,出现报错. 就过去看了一下,发现一个很陌生的错误提示: The operation co ...
- iOS 键盘遮挡输入 解决办法
.初始化及添加通知观察者 - (void)viewDidLoad { [super viewDidLoad]; self.tableView = [[UITableView alloc] initWi ...
- Map的性能
HashMap Map基于散列表的实现(它取代了Hashtable).插入和查询"键值对"的开销是固定的.可以通过构造器设置容量和负载因子,以调整容器的性能 LinkedHashM ...
- iOS 遇到的错误总结
1.[[[NSBundle mainBundle] loadNibNamed:@"UIFeedbackController" owner:nil options:nil] firs ...
- js get browser vertion (js获取浏览器信息版本)
1问题:js get browser vertion (js获取浏览器信息版本) 2解决方案 Copy this script into your JavaScript files. It works ...
- c和oc小知识
1.const const 修饰了*p1 / *p2 const int * p1=&age; int const * p2=&age;//和上面的意义一样 ,换句话说就是 在 “ * ...
- MAC下使用Charles抓取安卓模拟器数据
一.安装Charles,这个不多记录 二.Charles数据乱码问题(参照这篇文章 http://blog.csdn.net/huanghanqian/article/details/52973651 ...
- sqlserver2008附加数据库时提示“无法为该请求检索数据。 (Microsoft.SqlServer.Management.Sdk.Sfc)”
解决方案: 右击SQL Server Management Studio以管理员身份运行,选择与脱机数据库时相同的登陆方式(win还是sa),进入后再附加就是ok了.
- .net 文件上传大小的设置
直接在配置文件web.config 中进行如下配置,主要需要明白的就是 配置的 单位是 Byte, 所以一定计算清楚,不然会在这里纠结很久!!! <configuration> < ...