欢迎跟我一起学习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,sans-serif; font-weight: lighter; }
body { margin: 2em; }
启动应用
npm start 会自动启动浏览器,看到出现结果页面如下即表明启动成功: