原文发表于本人的我的博客,地址:Angular4的QuickStart——With ES6 Not TypeScript,欢迎反馈探讨。javascript
今年3月份,Angular官方发布了新版——Angular4.0.0,新版本的特性已经有不少文章了,在此不一一赘述。html
但从Angular2.x以来,JavaScript版本的官方文档就从未完整过,而ES6的QuickStart也从未在日程以内,这对初学者而言多少有点不太友好。虽然网上有ES6+Angular2.x的QuickStart,可是多少有点问题,并且跟Angular4有些不同,也过期了。今天为了折腾Angular4处处找文档查资料,搞了很久才搞定一个“hello world”,实在有些不爽,为了帮助像我同样的初学者,我以为把这个ES6的QuickStart写出来也许会有点用。java
NOTE:本文实现的内容与官方文档中的QuickStart实现的内容没有区别,只是本文是用ES6实现的,而非JavaScript或TypeScript.node
废话少说,show the code.webpack
首先是开发环境,先上package.json:git
//package.json { "name": "angular4-quickstart-es6-webpack", "version": "1.0.0", "description": "The Angular 4 Quickstart tutorial done in ES6 with webpack and Babel, without using TypeScript.", "scripts": { "start": "npm run lite", "lite": "lite-server", "webpack": "webpack --progress", "postinstall": "npm run webpack" }, "repository": { "type": "git", "url": "" }, "author": "", "license": "ISC", "bugs": { "url": "" }, "homepage": "", "dependencies": { "@angular/common": "^4.0.0", "@angular/compiler": "^4.0.0", "@angular/core": "^4.0.0", "@angular/forms": "^4.0.0", "@angular/http": "^4.0.0", "@angular/platform-browser": "^4.0.0", "@angular/platform-browser-dynamic": "^4.0.0", "@angular/router": "^4.0.0", "core-js": "^2.4.1", "rxjs": "^5.1.0", "zone.js": "^0.8.4", "es6-promise": "^3.0.2", "es6-shim": "^0.35.0", "reflect-metadata": "0.1.2" }, "devDependencies": { "babel-core": "^6.7.0", "babel-loader": "^6.2.4", "babel-preset-es2015": "^6.6.0", "concurrently": "^2.0.0", "lite-server": "^2.1.0", "script-loader": "^0.6.1", "webpack": "^1.12.14" } }
除了Angular官方文档中列出的包和工具以外,还包括了ES6的相关模块及其转码工具Babel。package.json配置好了以后就能够开始配置开发环境了,es6
目录结构:web
开发环境:npm
开发环境的配置须要安装Node(官网),而后安装webpack,json
npm install webpack -g
而后在nges6目录下执行:
npm install
或者执行:
cnpm install
安装时间可能会有点长,耐心等待,安装完成后,开发所须要的包就会被下载到node_modules文件夹中,接下来是webpack的配置文件:
//webpack.config.js var path = require('path'); module.exports = { entry: path.join(__dirname, 'public', 'src','index.js'), output: { path: path.join(__dirname, 'public', 'dist'), filename: 'vendor.js' }, devtool: 'source-map', module: { loaders: [ { test: /\.js$/, loader: "babel-loader", query: { presets: ['es2015'] } }, ] } }
配置文件中咱们定义了入口文件(public/src/index.js),而后定义了输出路径和输出文件的命名(public/dist/vendor.js),webpack将把咱们须要的所有依赖打包到一个文件中(vendor.js),这样咱们在HTML中只要用一个script标签引入这个文件就能够了(暂不考虑懒加载的问题)。
不知道上面这部分在说什么的同窗,请先以“nodejs”、“npm”、“webpack”等关键词搜索网上的资料。
组件
Angular从2.0开始组件化,关于组件,中文文档中介绍说:“组件是一个 Angular 类,用于把数据展现到视图 (view),并处理几乎全部的视图显示和交互逻辑。组件是 Angular 系统中最重要的基本构造块之一。”组件是一个应用的基础构成,因此,咱们首先在app.component.js中定义一个“组件”:
//app.component.js import {Component} from '@angular/core'; class AppComponent { static get annotations() { return [ new Component({ selector: "my-app", template: '<h1>My First Angular 2 App</h1>' }), ]; } constructor () {} } export {AppComponent};
在组件的定义中,须要用装饰器(Decorator)将组件的元数据附加到类上,而后Angular根据这些元数据建立一个组件实例。可是因为ES6目前不支持装饰器语法,所以经过static get annotations()方法完成这一工做。
模块:
Angular是以模块的形式来组织代码,每一个Angular应用都至少有一个根模块(Root Module),因此接下来是app.module.js:
//app.module.js import {BrowserModule} from '@angular/platform-browser'; import {NgModule} from '@angular/core'; import {AppComponent} from './app.component'; class AppModule{ static get annotations() { return [ new NgModule({ imports: [ BrowserModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) ]; } } export {AppModule};
首先用ES6中的import关键字导入依赖的包(platform-browser/core)以及咱们编写的组件(app.component),而后用class关键字声明咱们的根模块(AppModule)。关于impors、declarations、bootstrap的含义,请查阅官方文档中关于根模块的部分官方文档连接
启动引导:
在main.js中启动(bootstrap)根模块,这个引导文件基本上只写这一次就OK了:
//main.js import {platformBrowserDynamic} from '@angular/platform-browser-dynamic'; import {AppModule} from './app.module'; let boot = document.addEventListener('DOMContentLoaded', () => { platformBrowserDynamic().bootstrapModule(AppModule); }); module.exports = boot;
index.js
index.js用于告诉webpack须要打包的依赖都有哪些。
//index.js require('!!script!../../node_modules/es6-shim/es6-shim.min.js'); require('!!script!../../node_modules/core-js/client/shim.min.js'); require('!!script!../../node_modules/zone.js/dist/zone.js'); require('!!script!../../node_modules/rxjs/bundles/Rx.min.js'); require('!!script!../../node_modules/@angular/core/bundles/core.umd.js'); require('!!script!../../node_modules/@angular/common/bundles/common.umd.js'); require('!!script!../../node_modules/@angular/compiler/bundles/compiler.umd.js'); require('!!script!../../node_modules/@angular/platform-browser/bundles/platform-browser.umd.js'); require('!!script!../../node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js'); require('./app/main');
index.html
最后是index.html文件:
<!DOCTYPE html> <html> <head> <title>Angular 4 QuickStart</title> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <my-app>Loading...</my-app> <script type="text/javascript" src="public/dist/vendor.js"></script> </body> </html>
代码写好了,接下来是两个命令:
webpack --progress npm start
这两个命令会将全部须要的js模块打包到vendor.js中,并启动一个本地服务器,调用你的浏览器,你会看到你的应用跑起来了:
因为本人也是入门级选手,不少东西也没搞清楚,因此文章写得很简单。可是其中的诸多概念都有很多文章有所论述,能够在网上查到,我也不可能说得更明白,因此就不拾人牙慧了,更深刻的内容留待之后探讨。本文主要是提供一个基于webpack+ES6的QuickStart,若有问题,欢迎留言探讨。
如需转载,请注明原文连接。