第一个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 ...
随机推荐
- window下编辑了脚本文件linux报如下错误
首先vi :set ff 查看文件类型 接着 下载dos2unix root用户下yum -y install dos2unix 然后 dos2unix 文件.sh 转换格式 接着在正常启动即可
- Python面向对象——内置对象的功能扩展
1.扩展Python内置类 Python的数据类型 列表(list).字典(dict).集合(set).文件(file).字符串(str),这些都是对象 扩展list的功能,详解如图: 我们给列表添加 ...
- springaop——AspectJ不可不知的细节
springaop简介 springaop是spring对AOP技术的具体实现,它是spring框架的核心技术.springaop底层使用JDK动态代理或CGLIB动态代理技术实现. 应用场景: 在多 ...
- [原创]手把手教你写网络爬虫(4):Scrapy入门
手把手教你写网络爬虫(4) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 上期我们理性的分析了为什么要学习Scrapy,理由只有一个,那就是免费,一分钱都不用花! 咦?怎么有人扔西红柿 ...
- [LeetCode] Maximum Distance in Arrays 数组中的最大距离
Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from t ...
- [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- [LeetCode] Number Complement 补数
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- seacms6.5 注入漏洞1
---恢复内容开始--- 需要开启/data/admin/isapi.txt ,当里面的数值为1时,就可以报错注入 存在漏洞的页面:zyapi.php function cj() { global ...
- Java必须了解的“递归”与“IO流”!!!
>>>First: 递归! 1. 定义: 在函数自身内部,调用函数本身的方式,称为递归. 2. 注意: 递归包括递进去.归出来两步. 首先,依次执行[函数调自身语句]上半部分的代码, ...
- 推送本地项目至Github遇到的问题以及解决办法记录
在把本地新项目推送至GitHub仓库时的大致流程和步骤,首先现在GitHub上面新建一个项目,复制该项目的 带.git 后缀的地址,比如 git@github.com:XXX/XXX.git 然后在本 ...