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 { }
然後修改 app.module.ts ,修改之後代碼如下
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 { }
啓動你的應用
在app 目錄下新增 main.ts 文件,代碼如下
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 字樣
最後看一下目錄結構
忽略掉我畫橫線的文件,那是我後續新增的,
後面繼續學習代碼其中的含義和用法。over.