笔者在以前的开发流中习惯了Webpack+ES6+React的Workflow,Angular2自己也是秉持的模块的思想,所以笔者在学习Angular2的时候首先想到的也就是将本来流程里的React变为Angular2。Angular2官网上的Quick Start是用的TypeScript,准确的说,是AtScript的规范。Angular2自己引入了大量的第三方库,就像官方示例上面的,有JSPM的System、Reflect等等,这也给搭建环境时形成了必定的困扰。本节的代码笔者已经上传到了Github。Webpack的安装与基本使用就不在这边赘述,没学过的小伙伴能够看笔者其余的博文。javascript
|package.son 存放nom相关的配置 |webpack.config.js 存放webpack相关的配置 |assets 存放代码以及资源文件 ├── common 存放通用的资源文件 │ ├── font 字体文件 │ ├── css 通用样式文件 │ └── img 图片文件 ├── components 组件 │ ├── hello │ │ └── hello.js │ └── helloTemplate │ ├── helloTemplate.html │ └── helloTemplate.js ├── main.js 入口文件 ├── models 模型以及数据类 ├── services 服务 ├── utils 工具类 └── widgets 界面插件类
搭建环境须要大量的Npm依赖项,都列举在了下面:css
{ "name": "angular2-es6-webpack-quickstart", "version": "0.0.1", "description": "repository for starter with angular2,es6 and webpack", "main": "main.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "angular2", "es6", "webpack" ], "author": "chevalier", "license": "ISC", "dependencies": { "angular2": "^2.0.0-alpha.37", "reflect-metadata": "^0.1.1", "rx": "^2.5.3", "zone.js": "^0.5.4" }, "devDependencies": { "a1atscript": "^0.4.4", "autoprefixer-loader": "^2.0.0", "babel": "^5.8.23", "babel-core": "^5.8.23", "babel-loader": "^5.3.2", "css-loader": "^0.16.0", "file-loader": "^0.8.4", "image-webpack-loader": "^1.6.1", "imagemin": "^3.2.0", "node-sass": "^3.3.2", "react-mixin": "^1.7.0", "sass-loader": "^2.0.1", "scss-loader": "^0.0.1", "style-loader": "^0.12.3", "url-loader": "^0.5.6", "webpack": "^1.12.0", "typescript": "^1.6.0-beta", "typescript-simple-loader": "^0.3.7", "es6-shim": "^0.33.1", "ng-annotate": "^1.0.2", "raw-loader": "^0.5.1", "ng-annotate-webpack-plugin": "^0.1.2" } }
存放Webpack配置:html
var path = require('path'); module.exports = { entry: path.resolve(__dirname, 'assets/main.js'), // Config for our build files output: { path: path.resolve(__dirname, 'dist'), filename: "bundle.js" }, module: { loaders: [ {test: /\.jsx$/, exclude: /(libs|node_modules)/, loader: 'babel?stage=0'}, {test: /\.(es6|js)$/, exclude: /(libs|node_modules)/, loader: 'babel?stage=0'}, {test: /\.(png|jpg|ttf|woff|svg|eot)$/, loader: 'url-loader?limit=8192'},// inline base64 URLs for <=8k images, direct URLs for the rest { test: /\.css$/, loader: 'style-loader!css-loader!autoprefixer-loader?browsers=last 2 versions' }, // support for .html as raw text {test: /\.html$/, loader: 'raw'}, { test: /\.(scss|sass)$/, loader: 'style-loader!css-loader!autoprefixer-loader?browsers=last 2 versions!sass?sourceMap' }, { test: /\.ts$/, exclude: /(libs|node_modules)/, loader: 'typescript-simple', query: { 'ignoreWarnings': [ 2300, // 2300 -> Duplicate identifier 2309, // 2309 -> An export assignment cannot be used in a module with other exported elements. 2346, // 2346 -> Supplied parameters do not match any signature of call target. 2432 // 2432 -> In an enum with multiple declarations, only one declaration can omit an initializer for its first enum element. ] } } ], }, externals: { jquery: "jQuery", pageResponse: 'pageResponse' }, resolve: { alias: { libs: path.resolve(__dirname, 'libs'), nm: path.resolve(__dirname, "node_modules") } } };
环境搭建完毕,天然要开始写咱们的HelloWorld。这里组件的模块化以及引入都是用的ES6的语法,而后经过Webpack统一编译。利用Webpack一样能够支持AtScript的语法:java
hello.jsnode
/** * Created by apple on 15/9/14. */ import 'nm/reflect-metadata/Reflect.js'; import 'nm/zone.js/lib/zone.js'; import 'nm/es6-shim'; import { Component, View, bootstrap } from 'nm/angular2/angular2'; @Component({ selector: 'my-app' }) @View({ template: '<h1>Hello {{ name }}</h1>' }) class MyAppComponent { constructor() { this.name = 'World'; } } bootstrap(MyAppComponent);
main.jsreact
import {MyAppComponent} from "./components/hello/hello.js"; import {HelloTemplateComponent} from "./components/helloTemplate/helloTemplate.js"; import {Component, View, bootstrap} from 'nm/angular2/angular2'; bootstrap(MyAppComponent);
index.htmljquery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <my-app></my-app> </body> <script src="dist/bundle.js"></script> </html>
npm install http-server -g http-server
而后打开localhost:8080便可以看到Angular2为咱们渲染的新界面。webpack