Hello world
按步骤学习
- 安装node.js 和 npm
- 创建项目配置文件
- 创建一个根模块
- 创建一个根component
- 启动你的应用
- 定义一个网页
- 编译运行
安装node.js 和 npm
node 需要 v5.x.x npm 需要 3.x.x,可以分别使用使用 node -v 和 npm -v查看
创建项目配置文件
新增一个项目目录,比如 webapp,然后在 webapp 目录下面新增下面四个配置文件
- package.json 设置项目基本属性以及项目的依赖的npm 包
- tsconfig.json 定义将TypeScript写的源码如何生成JavaScript (这里使用TypeScript编写项目)
- typings.json 提供额外的定义,用来编码时可以智能识别(我是这么理解的,O(∩_∩)O)
- systemjs.config.js 设置程序模块地址,以及注册必须要的包
代码如下:
package.json
{
"name": "angular-helloworld","version": "1.0.0","scripts": { "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ","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","@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","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:dt/node#6.0.0+20160909174046" } }
然后在项目目录(webapp目录)下执行
npm install
创建一个根模块
一个angular 应用至少有一个模块
在webapp 目录下创建一个文件夹app
mkdir app
在app 文件夹下新增一个app.module.ts 代码如下
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
@NgModule({
imports: [ BrowserModule ]
})
export class AppModule { }
这是一个应用的入口,最简单的引用
创建一个根Component
在app 目录下新增 app.component.ts 代码如下
import { Component } from '@angular/core';
@Component({
selector: 'my-app',template: '<h1>Hello world</h1>'
})
export class AppComponent { }
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule ],declarations: [ AppComponent ],bootstrap: [ AppComponent ]
})
export class AppModule { }
启动你的应用
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
定义一个网页
在项目根目录 (webapp目录)下新增一个index.html,代码如下:
<html>
<head>
<title>Angular QuickStart</title>
<Meta charset="UTF-8">
<Meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="styles.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>
<my-app>Loading...</my-app>
</body>
</html>
core-js 是为了兼容老的浏览器
zone.js和 reflect-Metadata 是Angular 要求的
SystemJS 是负责模块加载的
systemjs.config.js 是SystemJS 的配置文件
编译运行
在webapp 目录下运行 命令
npm start
运行之后你将要看到 Hello world 字样