今天给你们介绍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来分析重点: