今天給你們介紹4種環境搭建的方法。javascript
官方指導文檔:www.angular.cn/guide/quickstartcss
請使用cnpm來安裝,或者配置淘寶鏡像。html
使用原生npm安裝可能會遇到的問題:前端
若是還遇到問題能夠參考:http://blog.csdn.net/zhy13087...java
Angular的種子文件,他有不少的版本,咱們今天經過官方的seed來安裝。node
官方的angular-seed地址:https://github.com/angular/an...python
步驟:webpack
前置需安裝的包文件git
seed文件的優勢:github
在開始搭建Angular環境前,還須要作一些準備工做,包括安裝Angular所依賴的基礎環境Node.js,能夠在官網(https://nodejs.org/en/download/)下載安裝,須要確認Node.js版本爲v4.x.x以上,npm版本爲v3.x.x以上。使用版本爲node v5.11.0版本。
1: 建立package.json
{ "name": "HelloByHand", "version": "1.0.0", "license": "MIT", "scripts": { "start": "npm run server", "server": "webpack-dev-server –inline –colors –progress –port 3000" }, "dependencies": { "@angular/common": "2.0.0", "@angular/compiler": "2.0.0", "@angular/core": "2.0.0", "@angular/platform-browser": "2.0.0", "@angular/platform-browser-dynamic": "2.0.0", "reflect-metadata": "~0.1.8", "core-js": "~2.4.1", "rxjs": "5.0.0-beta.12", "zone.js": "~0.6.26" }, "devDependencies": { "@types/core-js": "~0.9.34", "ts-loader": "~1.2.0", "webpack": "~1.12.9", "webpack-dev-server": "~1.14.0", "typescript": "~2.0.0" } }
2:建立tsconfig.json
配置了typescript編譯器的編譯參數。
{ "compileOnSave": true, "compilerOptions": { "module": "commonjs", "target": "es5", "sourceMap": true, "declaration": false, "moduleResolution": "node", "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments":false, "noImplicitAny": true, "suppressExcessPropertyErrors": true, "typeRoots": [ "node_modules/@types" ], "exclude": [ "node_modules" ] } }
3:建立資源文件夾
在項目根目錄下建立一個src文件夾4:建立組件相關文件
在src目錄下建立app.component.ts文件以及模板文件app.component.html,示例代碼以下:
//app.component.ts import { Component } from '@angular/core'; @Component({ selector: 'hello-world', templateUrl: 'scr/app.component.ts' }) export class AppComponent {} //app.component.html <h1>Hello World</h1>
5:建立app.module.ts文件
在Angular應用中須要用模塊來組織一些功能緊密相關的代碼塊,每一個應用至少有一個模塊,習慣上把它叫作AppModule。在src目錄下建立一個app.module.ts文件來定義AppModule,代碼以下:
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
6: 添加main.ts文件
Angular項目通常有一個入口文件,經過這個文件來串聯起整個項目。示例代碼以下:
//main.ts import 'reflect-metadata'; import 'zone.js'; import { platforBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app.module'; platforBrowserDynamic().bootstrapModule(AppModule).catch(err => console.log(err));
7:建立index.html宿主頁面
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>MyApp</title> <base href="/"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico"> </head> <body> <app-root>加載中....</app-root> <script src="bundle.js"></script> </body> </html>
8:建立webpack.config.js
//webpack.config.js var webpack = require('webpack'); var path = require('path'); module.exports = { entry: './scr/main.js' output: { filename: './bundle.js' }, resolve: { root: [path.join(__dirname, 'scr')], extensions: ['', '.ts', '.js'] }, module: { loaders: [ { test: /\.ts$/, loader: 'ts-loader' } ] } }
官方文檔:https://github.com/angular/an...
建立項目和組件:
http://chuantu.biz/t6/44/1505...
啓動服務:
測試和打包
特色跟特性:藉助了 Amber Cli (負責:項目構建、項目的組織架構等) / WebPack(負責:調試、打包、測試)
以Angular-cli建立的項目目錄爲基礎。
src 目錄下結構
咱們使用bootstrap的樣式,在控制層方面(bootstrap涉及到js的地方)咱們使用ngx-bootstrap。
官方地址:http://valor-software.com/ngx...涉及到javascript操做的部分在這個連接裏找解決方案。
AngularCli項目集成Bootstrap步驟:
須要掌握的內容:
第三個問題要從main.ts來分析重點: