第一个Angular2的样例
欢迎跟我一起学习Angular2
本文根据angular2官网手动敲码得来:
本文地址:http://blog.csdn.net/sushengmiyan
本文作者:苏生米沿
- 开发环境搭建
- 配置文件
- 安装依赖包
- *创建基础应用
- 创建组件
- 创建启动页面
- 编译启动
环境搭建
安装 Node.js and npm 。
nodejs 下载地址 https://nodejs.org/download输入 node - v 显示当前版本号
新版的NodeJS已经集成了npm,所以之前npm也一并安装好了。同样可以使用cmd命令行中键入 npm –v
配置文件
- package.json
- tsconfig.json
- typings.json
- systemjs.config.js
package.json代码块
如下:
{
"name": "angular-quickstart",
"version": "1.0.0",
"scripts": {
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
"lite": "lite-server",
"postinstall": "typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings"
},
"license": "ISC",
"dependencies": {
"@angular/common": "2.0.0",
"@angular/compiler": "2.0.0",
"@angular/core": "2.0.0",
"@angular/forms": "2.0.0",
"@angulartp": "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",
"systemjs": "0.19.27",
"zone.js": "^0.6.23",
"angular2-in-memory-web-api": "0.0.20",
"bootstrap": "^3.3.6"
},
"devDependencies": {
"concurrently": "^2.2.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.2",
"typings":"^1.3.2"
}
}
tsconfig.json代码块
如下:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
}
}
typings.json代码块
如下:
{
"globalDependencies": {
"core-js": "registry:dt/core-js#0.0.0+20160725163759",
"jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
"node": "registry:dtde#6.0.0+20160909174046"
}
}
typings.json代码块
如下:
/**
* 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/corendles/core.umd.js',
'@angular/common': 'npm:@angular/commonndles/common.umd.js',
'@angular/compiler': 'npm:@angular/compilerndles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browserndles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamicndles/platform-browser-dynamic.umd.js',
'@angulartp': 'npm:@angulartpndlestp.umd.js',
'@angular/router': 'npm:@angular/routerndles/router.umd.js',
'@angular/forms': 'npm:@angular/formsndles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular2-in-memory-web-api': 'npm:angular2-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'
},
'angular2-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
安装依赖包
在工程目录文件下运行npm install。
中间会比较慢,等待。没有出现npm ERR!就说明安装成功。
程序会自动安装typings。
即在项目文件夹下可以看到多出来的node_modules和typings文件夹。如果typings没有自动安装,则运行 npm run typings install 手动安装。
创建基础模块
在工程目录下创建一个app文件夹,创建一个根模块suos.module.ts文件,angular2的每个应用应该有一个根模块。
Suos.module.ts内容如下:
import { NgModule } from '@angular/core';
import{BrowserModule}from'@angular/platform-browser';
@NgModule({
imports: [ BrowserModule ]
})
export class SuosModule { }
创建基础组件
每个angular2应用至少由一个根组件。这里我们创建一个SuosComponent。在app目录下创建suos.component.ts 代码如下:
import { Component } from '@angular/core';
@Component({
selector: 'suosstart',
template: '<h1>Suos样例工程</h1>'
})
export class SuosComponent { }
创建启动类
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { SuosModule } from './suos.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(SuosModule);
创建启动页面
index.html
<html>
<head>
<title>Suos启动测试</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.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>
<suosstart>加载中...</suosstart>
</body>
</html>
其中style.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;
}
启动应用
npm start 会自动启动浏览器,看到出现结果页面如下即表明启动成功:
目录
第一个Angular2的样例的更多相关文章
- JBoss 系列九十六:JBoss MSC - 简介及一个简单演示样例
什么是 JBoss MSC JBoss MSC 即 JBoss Modular Service Container,是第三代 JBoss 产品 JBoss 7和WildFfly的内核,JBoss MS ...
- 使用CEF(二)— 基于VS2019编写一个简单CEF样例
使用CEF(二)- 基于VS2019编写一个简单CEF样例 在这一节中,本人将会在Windows下使用VS2019创建一个空白的C++Windows Desktop Application项目,逐步进 ...
- socket编程——一个简单的样例
从一个简单的使用TCP样例開始socket编程,其基本过程例如以下: server client ++ ...
- Yii学习笔记之二(使用gii生成一个简单的样例)
1. 数据库准备 (1) 首先我们建一数据库 yii2test 并建立一张表例如以下: DROP TABLE IF EXISTS `posts`; CREATE TABLE `posts` ( `po ...
- android一个上传图片的样例,包含怎样终止上传过程,假设在上传的时候更新进度条(一)
先上效果图: Layout为: <? xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- 一个简单的样例看明确怎样利用window.location.hash实现ajax操作时浏览器的前进/后退功能
我们知道JavaScript中非常早就提供了window.history对象,利用history对象的forward().go().back()方法可以方便实现不同页面之间的前进.后退等这样的导航功能 ...
- openWRT学习之LUCI之中的一个helloworld演示样例
备注1:本文 讲述的是原生的openWRT环境下的LUCI 备注2:本文參考了诸多资料.感谢网友分享.參考资料: http://www.cnblogs.com/zmkeil/archive/2013/ ...
- C#一个托付的样例
C#中的函数能够被声明的一个托付所调用. 函数为静态方法.和托付声明的參数返回值要一致. class Program { delegate float MathOperationDelegate( ...
- 一个简单演示样例来演示用PHP訪问表单变量
首先编写表单页面orderform.html,用post方法请求服务端脚本文件:processorder.php orderform.html: <!DOCTYPE html> <h ...
随机推荐
- 【省带宽、压成本专题】从产品架构来看,PCDN如何节流50%
过去几年,我们一直在视频省流量方面潜心钻研,取得不俗的成果.本次"省带宽.压成本"系列一共会推出六篇文章,从技术迭代.硬件更新等角度出发,向大家介绍节省CDN流量,降低视频播放成本 ...
- How to preview html file in our browser at sublime text?
sublime preview html.md open In Browser what should we do if we want to preview html file in our bro ...
- js 一些基础的理解
javascript(JS)的组成? DOM 文档对象模型 BOM 浏览器对象模型 ECMAScript javascript(JS)在页面中处理了什么事情? 特效交互 数据交互 逻辑操作 常见特效的 ...
- VINS 估计器之结构初始化
为什么要初始化 非线性VINS估计器的性能对于初始的速度,尺度,重力向量,空间点3D位置,以及外参等非常敏感.在很多场合中,能做到相机和IMU即插即用,线上自动校准与初始化,将会给用户带来极大的方便性 ...
- [NOIp 2012]国王游戏
Description 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国 ...
- poj3728 商务旅行
[Description]小 T 要经常进行商务旅行,他所在的国家有 N 个城镇,标号为 1,2,3,...,N,这 N 个城镇构成一棵树.每个城镇可以买入和卖出货物,同一城镇买入和卖出的价格一样,小 ...
- codeforces round #419 E. Karen and Supermarket
On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...
- Educational Codeforces Round 18
A. New Bus Route 题目大意:给出n个不同的数,问差值最小的数有几对.(n<=200,000) 思路:排序一下,差值最小的一定是相邻的,直接统计即可. #include<cs ...
- 【bzoj4571 scoi2016】美味
题目描述 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1<=i<=n).有 m 位顾客,第 i 位顾客的期望值为 bi,而他的偏好值为 xi .因此,第 ...
- Python Django的分页,Form验证,中间件
本节内容 Django的分页 Form 中间件 1 Django 分页 1.1 Django自带的分页 1.首先来看下我的测试数据环境 ############ models.py ######### ...