初探angular2

Angular2 是一款开源JavaScript库,由Google维护,用来协助单一页面应用程序运行。

Angular2 是 Angular 1.x 的升级版本,性能上得到显著的提高,能很好的支持 Web 开发组件。

Angular2 发布于2016年9月份,它是基于ES6来开发的。

上手一个新的框架,了解的最开始的东西,都是最浅显的。

我的第一个应用如下

初探angular2

查看项目目录

初探angular2

实现项目过程如下

$ mkdir angular-quickstart

$ cd angular-quickstart

创建 package.json 文件

package.json
{
"name": "angular2-quickstart",
"version": "1.0.0",
"scripts": {
"start": "npm run lite",
"lite": "lite-server"
},
"license": "ISC",
"dependencies": {
"@angular/common": "2.0.0",
"@angular/compiler": "2.0.0",
"@angular/core": "2.0.0",
"@angular/forms": "2.0.0",
"@angular/http": "2.0.0",
"@angular/platform-browser": "2.0.0",
"@angular/platform-browser-dynamic": "2.0.0",
"@angular/router": "3.0.0",
"@angular/upgrade": "2.0.0", "core-js": "^2.4.1",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.12",
"zone.js": "^0.6.23", "angular2-in-memory-web-api": "0.0.20",
"bootstrap": "^3.3.6"
},
"devDependencies": {
"concurrently": "^2.0.0",
"lite-server": "^2.2.0"
}
}

$ npm install -g cnpm --registry=https://registry.npm.taobao.org

$ cnpm install

$ mkdir app

$ cd app

app.component.js
(function(app){
app.AppComponent=
ng.core.Component({
selector:'my-app',
template:'<h1>我的第一个Angular应用</h1>'
})
.Class({
constructor:function(){}
})
})(window.app||(window.app={}));

Angular应用都是模块化的,ES5没有内置的模块化系统,可以使用第三方模块系统,然后我们为应用创建独立的命名空间 app,

文件代码可以包裹在 IIFE(立即执行函数表达式)中:

(function(app) {
})(window.app || (window.app = {}));
app.module.js
(function(app) {
app.AppModule =
ng.core.NgModule({
imports: [ ng.platformBrowser.BrowserModule ],
declarations: [ app.AppComponent ],
bootstrap: [ app.AppComponent ]
})
.Class({
constructor: function() {}
});
})(window.app || (window.app = {}));
main.js
(function(app){
document.addEventListener('DOMContentLoaded',function(){
ng.platformBrowserDynamic
.platformBrowserDynamic()
.bootstrapModule(app.AppModule);
});
})(window.app||(window.app={}))

我们需要两样东西来启动应用:

Angular 的 platformBrowserDynamic().bootstrapModule 函数。

上文中提到的应用根模块 AppModule。

接下来创建 index.html,代码如下所示:

index.html
<html> <head>
<meta charset="utf-8">
<title>Angular 2 实例 - 菜鸟教程(runoob.com)</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css"> <!-- 1. 载入库 -->
<!-- IE 需要 polyfill -->
<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/rxjs/bundles/Rx.js"></script>
<script src="node_modules/@angular/core/bundles/core.umd.js"></script>
<script src="node_modules/@angular/common/bundles/common.umd.js"></script>
<script src="node_modules/@angular/compiler/bundles/compiler.umd.js"></script>
<script src="node_modules/@angular/platform-browser/bundles/platform-browser.umd.js"></script>
<script src="node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js"></script> <!-- 2. 载入 'modules' -->
<script src='app/app.component.js'></script>
<script src='app/app.module.js'></script>
<script src='app/main.js'></script> </head> <!-- 3. 显示应用 -->
<body>
<my-app>Loading...</my-app>
</body> </html>

index.html 分析

1、载入我们需要的JavaScript库;

2、载入我们自己的JavaScript文件,注意顺序;

3、我们在标签中添加标签。

执行过程为:当 Angular 在 main.js 中调用 bootstrapModule 函数时,它读取 AppModule 的元数据,在启动组件中找到 AppComponent 并找到 my-app 选择器,

定位到一个名字为 my-app 的元素,然后再这个标签之间的载入内容。

添加一些样式

styles.css 文件代码为

styles.css
h1 {
color: #369;
font-family: Arial, Helvetica, sans-serif;
font-size: 250%;
}
body {
margin: 2em;
}

最后输入

$ npm start

第二种方法,我们也可以使用angular-cli构建项目应用

初探angular2

初始化命令如下

cnpm install -g @angular/cli

ng new my-app

cd my-app

ng serve --open

ng serve 命令会启动开发服务器,监听文件变化,并在修改这些文件时重新构建此应用。

by本文学习自菜鸟教程 http://www.runoob.com/angularjs2/angularjs2-javascript-setup.html

上一篇:SpringBoot整合Quartz作为调度中心完整实用例子


下一篇:/proc/sys/vm/drop_caches 清理缓存