1、dev.js:

const webpack = require('webpack');
const webpackUglifyJsPlugin = require('webpack-uglify-js-plugin');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
function root(__path) {
return path.join(__dirname, __path);
}
const config = {
entry: [
"webpack-hot-middleware/client?reload=true",
// 这里是你的入口文件
"./src/index.js",
],
output: { //输出目录
publicPath: "",
path: __dirname,
filename: 'bundle.js',
},
module: {
rules: [{
test: /\.jsx?$/,
use: [{
loader: 'babel-loader', //只需要babel就可以写ng2的代码了
options: {
presets: ['es2015', 'es2016', 'es2017', 'stage-0'], //使用的presets
plugins: ['transform-decorators-legacy'] //使用的babel插件
}
}],
exclude: /node_modules/
}, {
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
loader: "css-loader!autoprefixer-loader?{browsers:['last 6 Chrome versions', 'last 3 Safari versions', 'iOS >= 5', 'Android >= 4.0']}!sass-loader",
}),
}, {
test: /\.png$/,
use: {
loader: 'file-loader',
options: {
name: '../img/[name].[ext]'
}
}
}]
},
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.ContextReplacementPlugin( //这个是ng需要用的插件,以免报错
// The (\\|\/) piece accounts for path separators in *nix and Windows
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
root('./src'), // location of your src
{} // a map of your routes
),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new ExtractTextPlugin('css/style.css')
/*new HtmlWebpackPlugin({
title: 'index',
hash:true,
template: 'index.ejs', // Load a custom template (ejs by default see the FAQ for details)
})*/
]
};
module.exports = config;

2、server.js:

var webpack = require('webpack'),
webpackDevMiddleware = require('webpack-dev-middleware'),
webpackHotMiddleware = require('webpack-hot-middleware'),
config = require("./config/dev.js"),
express = require('express'),
app = express(),
compiler = webpack(config);
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath,
noInfo: true,
stats: {
colors: true,
progress: true
}
}));
app.use(webpackHotMiddleware(compiler, {
noInfo: true,
publicPath: config.output.publicPath
})); app.get('*', function(req, res) {
var fileName = req.url;
console.log(fileName);
if (fileName == '/') {
res.sendFile(__dirname + '/index.html');
}else{
res.sendFile(__dirname + fileName);
}
});
app.listen(8087,'0.0.0.0');

3、package.json:

{
"name": "wtest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node server.js",
"build": "NODE_ENV=production&&npm run output",
"output": "webpack --config webpack.build.js",
"test": "node ./dist/test.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@angular/common": "^2.1.0",
"@angular/compiler": "^2.1.0",
"@angular/core": "^2.1.0",
"@angular/http": "^2.1.0",
"@angular/platform-browser": "^2.1.0",
"@angular/platform-browser-dynamic": "^2.1.0",
"@angular/router": "^3.1.0",
"es6-promise": "3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "0.1.8",
"rxjs": "^5.2.0",
"zone.js": "^0.6.26",
"autoprefixer-loader": "^3.2.0",
"babel-core": "^6.23.1",
"babel-loader": "^6.3.2",
"babel-plugin-angular2-annotations": "^5.1.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-angular2": "^0.0.2",
"babel-preset-es2015": "^6.22.0",
"babel-preset-es2016": "^6.22.0",
"babel-preset-es2017": "^6.22.0",
"babel-preset-react": "^6.23.0",
"babel-preset-stage-0": "^6.22.0",
"copy-webpack-plugin": "^4.0.1",
"css-loader": "^0.26.2",
"extract-text-webpack-plugin": "^2.0.0",
"file-loader": "^0.10.1",
"html-webpack-plugin": "^2.28.0",
"node-sass": "^4.5.0",
"reload": "^1.1.1",
"sass-loader": "^6.0.2",
"style-loader": "^0.13.2",
"uglifyjs-webpack-plugin": "^0.3.0",
"webpack": "^2.2.1",
"webpack-del-plugin": "^0.0.1",
"webpack-dev-middleware": "^1.10.1",
"webpack-dev-server": "^2.4.1",
"webpack-hot-middleware": "^2.17.1",
"webpack-spritesmith": "^0.3.1",
"webpack-uglify-js-plugin": "^1.1.9"
}
}

4、index.js:

import 'reflect-metadata';
import 'zone.js'; //这两个是为了兼容angular2正常使用而导入的插件
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; // import { enableProdMode } from '@angular/core'; import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser'; import App from './test.js';
import Test from "./test2.js";
@NgModule({
imports: [ BrowserModule ],
declarations: [
App,
Test //声明所有的组件,这样才能使用组建
],
bootstrap: [ App ] //启动组建,这样index.html 中才能使用该标签
})
class AppModule { } platformBrowserDynamic().bootstrapModule(AppModule);

5、test.js:

import { Component } from '@angular/core';
import Test from "./test2.js"; @Component({
selector: 'my-app',
template: `
<h1>sdfjsdfkj</h1>
<h2>Have {{ what }}</h2>
<test-app [toChildData]="toChildData"></test-app>
`
})
export default class App {
constructor() {
this.what = "a good time!";
this.toChildData='sdfksdfdjs';
}
};

6、test2.js:

import {
Component,
Input
} from '@angular/core'; @Component({
selector: 'test-app',
template: `
<div (click)="testfn()">{{testData}}</div> //事件促发案例
`
}) export default class Test {
@Input() toChildData;
constructor() {
this.testData = '46456654';
}
testfn() {
console.log(this.toChildData);
alert('dflksjdfj')
}
}

  

最新文章

  1. 【积累篇:他山之石,把玉攻】Mime 类型列表
  2. SQLite剖析之设计与概念
  3. centOS目录结构
  4. [芯片][MPU6050] MPU60X0的DMP相关链接
  5. NSQ:分布式消息队列学习记录
  6. 2015-11-04 报表(c#部分)(Datatable 查询,弹出日期控件,输入是否整数)
  7. backbone todo example
  8. devenv.exe - Assert Failure visual studio 2010
  9. POJ 2186.Popular Cows (强连通)
  10. PHP中cURL错误号对照[转]
  11. T-SQL问题解决集锦——数据加解密(2)
  12. 词组缩写(isalpha()的应用)
  13. python 机器学习 决策树
  14. Linux 零拷贝技术
  15. TCP的核心系列 — SACK和DSACK的实现(四)
  16. vue组件通信那些事儿
  17. 2017-2018-2 20165315 实验四《Android程序设计》实验报告
  18. bind的封装
  19. was修改控制台端口教程
  20. hdu 5773 The All-purpose Zero 线段树 dp

热门文章

  1. ASP.NET WebApi使用Swagger生成api说明文档
  2. oracle-scn
  3. docker nginx 运行后无法访问
  4. TestNG.xml参数配置-如何控制部分执行@test方法
  5. 红外协议之NEC协议
  6. lsof根据端口返回进程号杀死进程的方法
  7. 亚马逊aws 一个实例双网卡-两个弹性ip设置
  8. excel技巧--文本拆分合并
  9. 关于PHP程序员技术职业生涯规划 转自 韩天锋
  10. 构建Jenkins自动化编译管理环境