Vue.js+Jquery+Webpack初体验
1、安装node.js运行环境,直接到官网下载相应的安装包即可,安装完后运行node -v和npm -v看下版本号
D:\web\webpack-example>node -v
v10.15.3
D:\web\webpack-example>npm -v
6.4.1
2、创建web项目,安装webpack运行环境、Vue.js和Jquery
D:\web\webpack-example>npm init -y
Wrote to D:\web\webpack-example\package.json:
{
"name": "webpack-example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
npm install webpack webpack-cli --save-dev
npm install vue
npm install expose-loader --save
npm install jquery --save
执行以上命令后,生成了项目的基本目录结构
3、创建src目录和源文件
<!--index.html-->
<!DOCTYPE html>
<html>
<head>
<title>Vue.js,Jquery,Webpack</title>
</head>
<body>
<div class="main" id="app">
<div class="head" v-html="message"></div>
<div class="content">
<div id="menu" class="nav" v-bind:class="{nav: menuOpen, 'nav-close': !menuOpen}">
<a class="menu-open-handler" href="javascript:;" v-on:click="openMenu">打开菜单</a>
<a class="menu-close-handler" href="javascript:;" v-on:click="closeMenu">关闭菜单</a>
<ul>
<li v-for="m in menus" v-bind:id="'menu'+m.id">{{ m.text }}</li>
</ul>
</div>
<div class="content-wrapper"></div>
</div>
</div>
</body>
</html>
/*style.css*/
html, body{margin: 0; padding: 0; font-family: 'Microsoft Yahei'; height: 100%;}
a{text-decoration: none; color: grey;}
.main{width: 1200px; height: 100%; margin: 0 auto; background: #ddd; display: flex; flex-direction: column; flex: auto;}
.head{height: 60px; background: #ddd; position: relative; font-size: 2.0em;}
.content{background: pink; display: flex; flex: auto;}
.nav{width: 220px; background: silver; position: relative;}
.nav ul{list-style:none; width: 180px; margin: 30px 0; padding: 0;}
.nav ul li{cursor: pointer; width: 100%; line-height: 32px; color: grey; text-indent: 15px;}
.nav ul li:hover{color: orange; background: #ddd;}
.nav-close{width: 40px; background: silver; position: relative;}
.nav-close ul{display: none;}
.menu-open-handler{width: 90px; transform: rotate(90deg); position: absolute; left: -20px; top: 40px;}
.menu-close-handler{width: 90px; transform: rotate(-90deg); position: absolute; right: -20px; top: 20px;}
.content-wrapper{height: 100%;background: green;}
//index.js
import Vue from 'vue/dist/vue.esm.js';
require('./style.css');
require("expose-loader?$!jquery");
var app = new Vue({
el: "#app",
data: {
message: '<font color="orange">Webpack</font> & <font color="green">Vue.js</font>',
menus: [
{id: 1, text: '菜单1'},
{id: 2, text: '菜单2'},
{id: 3, text: '菜单3'},
{id: 4, text: '菜单4'}
],
menuOpen: true
},
methods: {
openMenu: function(){
console.log('open menu');
$("#menu").animate({width: '220px'});
$("#menu").find("ul").show();
$("#menu").find("a.menu-open-handler").hide();
$("#menu").find("a.menu-close-handler").show();
},
closeMenu: function(){
console.log('open menu');
$("#menu").animate({width: '40px'});
$("#menu").find("ul").hide();
$("#menu").find("a.menu-close-handler").hide();
$("#menu").find("a.menu-open-handler").show();
}
},
created: function(){
if(this.menuOpen){
$("#menu").find("a.menu-open-handler").hide();
$("#menu").find("a.menu-close-handler").show();
}else{
$("#menu").find("a.menu-close-handler").hide();
$("#menu").find("a.menu-open-handler").show();
}
}
});
4、配置webpack,安装dev-server进行调试
首先,安装2个插件clean-webpack-plugin和html-webpack-plugin用于清理output和生成html文件
npm install clean-webpack-plugin --save-dev
npm install html-webpack-plugin --save-dev
因为需要打包css文件,所以还需要安装css-loader和style-loader
npm install css-loader style-loader --save-dev
项目中还会安装webpack-dev-server来预览效果
npm install webpack-dev-server --save-dev
修改package.json,新增2个scripts,用于启动dev-server和build
{
"name": "webpack-example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --open",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^4.30.0",
"webpack-cli": "^3.3.2"
}
}
新增webpack.config.js
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
index: './src/index.js'
},
devtool: 'inline-source-map',
devServer: {
contentBase: './dist'
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: './src/index.html'
})
],
output: {
filename: '[name].dist.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
}]
}
}
接下来运行npm run build来打包项目,如果没有报错的话,dist目录下会生成index.html和index.dist.js两个文件,此时就可以将dist内容发布到服务器了。
最后将项目上传到github,已经安装了git bash, 项目目录右键- Git bash here
git init
git add .
git status
git commit -m 'first commit'
git remote add origin https://github.com/zqiangliu/webpack-example.git
git push -u origin master
开发调试热加载可以npm start启动webpack-dev-server来预览效果。查看效果