詳解基于webpack2.x的vue2.x的多頁面站點(diǎn)
本文介紹了基于webpack2.x的vue2.x的多頁面站點(diǎn),分享給大家,具體如下:
vue的多頁面
依舊使用vue-cli來初始化我們的項(xiàng)目
然后修改主要目錄結(jié)構(gòu)如下:
├── build │ ├── build.js │ ├── check-versions.js │ ├── dev-client.js │ ├── dev-server.js │ ├── utils.js │ ├── vue-loader.conf.js │ ├── webpack.base.conf.js │ ├── webpack.dev.conf.js │ └── webpack.prod.conf.js ├── src │ ├── pages │ │ ├── boys │ │ │ ├── index.html │ │ │ ├── index.js │ │ │ └── index.vue │ │ ├── goods │ │ │ ├── index.html │ │ │ ├── index.js │ │ │ └── index.vue │ │ ├── index │ │ │ ├── index.html │ │ │ ├── index.js │ │ │ └── index.vue │ │ └── sotho │ │ ├── index.html │ │ ├── index.js │ │ └── index.vue
編寫每個(gè)頁面
可以看到這里我們有4個(gè)單獨(dú)的頁面,分別是boys,goods,index,sotho
首先看boys文件夾中的代碼:
index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>vue3</title> </head> <body> <div id="app"></div> <!-- built files will be auto injected --> </body> </html>
這個(gè)是我們要單獨(dú)生成的頁面,最后也是生成index.html
index.vue
<style scoped>
.boys {
background-color: red;
}
</style>
<template>
<div id="app" class="boys">
boys got many things.
</div>
</template>
<script>
export default {
name: 'boys'
}
</script>
這是我們的vue文件,可以看成一個(gè)組件,其實(shí).vue文件你可以看成一個(gè)語法糖,最終會(huì)被vue-loader編譯成js,生成對應(yīng)的css,js,dom
index.js
import Vue from 'vue'
import Index from './index.vue'
Vue.config.productionTip = false
new Vue({
el: '#app',
template: '<Index/>',
components: { Index }
})
這就是主要文件了,這里執(zhí)行vue的實(shí)例化,用法同在瀏覽器端頁面中直接引入vue.js文件一樣的含義
其他幾個(gè)頁面一樣也是同理,具體可以見:
修改webpack.config.js
由于vue中的配置使用了模塊化管理,所以我們需要修改下面兩個(gè)文件:
1、webpack.base.conf.js
我們需要修改webpack.base.conf.js的入口entry,這是配置多入口文件的重點(diǎn)!
如果不懂多入口含義的化,建議去看下webpack的文檔。
webpack.base.conf.js
...
module.exports = {
entry: {
'pages/boys/index': './src/pages/boys/index.js', //配置boys頁面入口
'pages/goods/index': './src/pages/goods/index.js', //配置goods頁面入口
'pages/index/index': './src/pages/index/index.js', //配置index頁面入口
'pages/sotho/index': './src/pages/sotho/index.js', //配置sotho頁面入口
},
...
2、webpack.dev.conf.js
這里我們需要修改plugins,它是個(gè)強(qiáng)大的即插即用的拓展。
我們使用html-webpack-plugin來生成我們的對于的頁面。
...
var HtmlWebpackPlugin = require('html-webpack-plugin')
...
...
module.exports = merge(baseWebpackConfig, {
...
plugins: [
new webpack.DefinePlugin({
'process.env': config.dev.env
}),
new HtmlWebpackPlugin({
filename:'./pages/boys/index.html', //指定生成的html存放路徑
template:'./src/pages/boys/index.html', //指定html模板路徑
inject: true, //是否將js等注入頁面,以及指定注入的位置'head'或'body'
chunks:['pages/boys/index'] //需要引入的chunk(模塊資源),不配置就會(huì)引入所有頁面的資源(js/css),這是個(gè)很重要的屬性,你可以不配置試試效果
}),
new HtmlWebpackPlugin({
filename:'./pages/goods/index.html',
template:'./src/pages/goods/index.html',
inject: true,
chunks:['pages/goods/index']
}),
new HtmlWebpackPlugin({
filename:'./pages/index/index.html',
template:'./src/pages/index/index.html',
inject: true,
chunks:['pages/index/index']
}),
new HtmlWebpackPlugin({
filename:'./pages/sotho/index.html',
template:'./src/pages/sotho/index.html',
inject: true,
chunks:['pages/sotho/index']
}),
...
]
})
以上就是我們進(jìn)行多頁開發(fā)的主要配置項(xiàng)。
開發(fā)環(huán)境訪問頁面
運(yùn)行npm run dev,我們看下怎么訪問我們的多頁vue應(yīng)用。
- http://localhost:9090/pages/index/index.html 訪問index頁面
- http://localhost:9090/pages/boys/index.html 訪問boys頁面
- http://localhost:9090/pages/goods/index.html 訪問goods頁面
- http://localhost:9090/pages/sotho/index.html 訪問sotho頁面
再來看下我們的dom結(jié)構(gòu)是什么樣:

頁面里的js就是我們通過插件注入的,并且我們是通過指定chunks完成。
build
運(yùn)行npm run build
➜ vue2-x-multiple git:(master) ✗ npm run build
> vue3@1.0.0 build /study/vue2-x-multiple
> node build/build.js
⠋ building for production...
Starting to optimize CSS...
Processing static/css/pages/boys/index.19ebbc80a1c187989dbf02d03192e84e.css...
Processing static/css/pages/goods/index.fe8f1bc39f33dce4c4d610c2326482c6.css...
Processing static/css/pages/index/index.f6340f14071a89cf2b092da280ffaf8c.css...
Processing static/css/pages/sotho/index.7415ffd3ef7b9d1a4398cba49927b12b.css...
Processed static/css/pages/boys/index.19ebbc80a1c187989dbf02d03192e84e.css, before: 114, after: 44, ratio: 38.6%
Processed static/css/pages/goods/index.fe8f1bc39f33dce4c4d610c2326482c6.css, before: 116, after: 46, ratio: 39.66%
Processed static/css/pages/index/index.f6340f14071a89cf2b092da280ffaf8c.css, before: 92, after: 22, ratio: 23.91%
Processed static/css/pages/sotho/index.7415ffd3ef7b9d1a4398cba49927b12b.css, before: 92, after: 22, ratio: 23.91%
Hash: 2467c91090ccf4690865
Version: webpack 2.5.1
Time: 6319ms
Asset Size Chunks Chunk Names
static/css/pages/sotho/index.7415ffd3ef7b9d1a4398cba49927b12b.css.map 312 bytes 1 [emitted] pages/sotho/index
static/js/vendor.d7548891d04d4f883b29.js 83.2 kB 0 [emitted] vendor
static/js/pages/index/index.b2ce74f4155fb942a064.js 671 bytes 2 [emitted] pages/index/index
static/js/pages/goods/index.7d0dda2791db2d3b1500.js 702 bytes 3 [emitted] pages/goods/index
static/js/pages/boys/index.2c268b75ba9424211d79.js 699 bytes 4 [emitted] pages/boys/index
static/js/manifest.f466ccb58b3271558be5.js 1.57 kB 5 [emitted] manifest
static/css/pages/boys/index.19ebbc80a1c187989dbf02d03192e84e.css 44 bytes 4 [emitted] pages/boys/index
static/css/pages/goods/index.fe8f1bc39f33dce4c4d610c2326482c6.css 46 bytes 3 [emitted] pages/goods/index
static/css/pages/index/index.f6340f14071a89cf2b092da280ffaf8c.css 22 bytes 2 [emitted] pages/index/index
static/css/pages/sotho/index.7415ffd3ef7b9d1a4398cba49927b12b.css 22 bytes 1 [emitted] pages/sotho/index
static/js/vendor.d7548891d04d4f883b29.js.map 687 kB 0 [emitted] vendor
static/js/pages/sotho/index.e706490d7c42ad8e4f73.js.map 5.55 kB 1 [emitted] pages/sotho/index
static/js/pages/sotho/index.e706490d7c42ad8e4f73.js 674 bytes 1 [emitted] pages/sotho/index
static/js/pages/index/index.b2ce74f4155fb942a064.js.map 5.55 kB 2 [emitted] pages/index/index
static/css/pages/index/index.f6340f14071a89cf2b092da280ffaf8c.css.map 312 bytes 2 [emitted] pages/index/index
static/js/pages/goods/index.7d0dda2791db2d3b1500.js.map 5.64 kB 3 [emitted] pages/goods/index
static/css/pages/goods/index.fe8f1bc39f33dce4c4d610c2326482c6.css.map 338 bytes 3 [emitted] pages/goods/index
static/js/pages/boys/index.2c268b75ba9424211d79.js.map 5.62 kB 4 [emitted] pages/boys/index
static/css/pages/boys/index.19ebbc80a1c187989dbf02d03192e84e.css.map 333 bytes 4 [emitted] pages/boys/index
static/js/manifest.f466ccb58b3271558be5.js.map 14.6 kB 5 [emitted] manifest
./pages/boys/index.html 386 bytes [emitted]
./pages/goods/index.html 389 bytes [emitted]
./pages/index/index.html 389 bytes [emitted]
./pages/sotho/index.html 389 bytes [emitted]
Build complete.
Tip: built files are meant to be served over an HTTP server.
Opening index.html over file:// won't work.
進(jìn)入dist目錄,查看生成的頁面
├── pages
│ ├── boys
│ │ └── index.html
│ ├── goods
│ │ └── index.html
│ ├── index
│ │ └── index.html
│ └── sotho
│ └── index.html
└── static
├── css
│ └── pages
│ ├── boys
│ │ ├── index.19ebbc80a1c187989dbf02d03192e84e.css
│ │ └── index.19ebbc80a1c187989dbf02d03192e84e.css.map
│ ├── goods
│ │ ├── index.fe8f1bc39f33dce4c4d610c2326482c6.css
│ │ └── index.fe8f1bc39f33dce4c4d610c2326482c6.css.map
│ ├── index
│ │ ├── index.f6340f14071a89cf2b092da280ffaf8c.css
│ │ └── index.f6340f14071a89cf2b092da280ffaf8c.css.map
│ └── sotho
│ ├── index.7415ffd3ef7b9d1a4398cba49927b12b.css
│ └── index.7415ffd3ef7b9d1a4398cba49927b12b.css.map
└── js
├── manifest.f466ccb58b3271558be5.js
├── manifest.f466ccb58b3271558be5.js.map
├── pages
│ ├── boys
│ │ ├── index.2c268b75ba9424211d79.js
│ │ └── index.2c268b75ba9424211d79.js.map
│ ├── goods
│ │ ├── index.7d0dda2791db2d3b1500.js
│ │ └── index.7d0dda2791db2d3b1500.js.map
│ ├── index
│ │ ├── index.b2ce74f4155fb942a064.js
│ │ └── index.b2ce74f4155fb942a064.js.map
│ └── sotho
│ ├── index.e706490d7c42ad8e4f73.js
│ └── index.e706490d7c42ad8e4f73.js.map
├── vendor.d7548891d04d4f883b29.js
└── vendor.d7548891d04d4f883b29.js.map
到此為止,一個(gè)簡單的基于vue2.x的多頁應(yīng)用完成了。
升級
webpack.base.conf.js中的entry入口都是手工寫入,如果頁面多的話這樣肯定有問題。
所以我們通過如下的方式來自動(dòng)完成這段代碼:
var entryJS = glob.sync('./src/pages/**/*.js').reduce(function (prev, curr) {
prev[curr.slice(6, -3)] = curr;
return prev;
}, {});
這里的 './src/pages/**/*.js' 我們按照一定的規(guī)則去匹配我們的入口j s即可。
生成的的是:
{ 'pages/boys/index': './src/pages/boys/index.js',
'pages/goods/index': './src/pages/goods/index.js',
'pages/index/index': './src/pages/index/index.js',
'pages/sotho/index': './src/pages/sotho/index.js' }
與我們想要的行為一致
同樣我們也升級下我們的webpack.dev.conf.js中的plugins
var htmls = glob.sync('./src/pages/**/*.html').map(function (item) {
return new HtmlWebpackPlugin({
filename: './' + item.slice(6),
template: item,
inject: true,
chunks:[item.slice(2, -5)]
});
});
module.exports = merge(baseWebpackConfig, {
module: {
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
},
// cheap-module-eval-source-map is faster for development
devtool: '#cheap-module-eval-source-map',
plugins: [
new webpack.DefinePlugin({
'process.env': config.dev.env
}),
// https://github.com/glenjamin/webpack-hot-middleware#installation--usage
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
// https://github.com/ampedandwired/html-webpack-plugin
new FriendlyErrorsPlugin()
].concat(htmls)
})
生成的是:
HtmlWebpackPlugin {
options:
{ template: './src/pages/boys/index.html',
filename: './pages/boys/index.html',
hash: false,
inject: true,
compile: true,
favicon: false,
minify: false,
cache: true,
showErrors: true,
chunks: [ 'pages/boys/index' ],
excludeChunks: [],
title: 'Webpack App',
xhtml: false } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Three.js開發(fā)實(shí)現(xiàn)3D地圖的實(shí)踐過程總結(jié)
這篇文章主要給大家介紹了關(guān)于利用Three.js開發(fā)實(shí)現(xiàn)3D地圖的實(shí)踐過程,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用three.js具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
解讀Bootstrap v4 sass設(shè)計(jì)
這篇文章主要介紹了Bootstrap v4 sass設(shè)計(jì)的相關(guān)資料,需要的朋友可以參考下2016-05-05
layui+ssm實(shí)現(xiàn)數(shù)據(jù)批量刪除功能
本篇文章給大家介紹layui+ssm實(shí)現(xiàn)數(shù)據(jù)批量刪除功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12
提交表單時(shí)執(zhí)行func方法實(shí)現(xiàn)代碼
客戶端的js驗(yàn)證想必大家早已熟悉,今天本文帶著大家再回憶一下,主要是在提交表單之前執(zhí)行func方法,感興趣的你可以參考下哈,希望可以幫助到你2013-03-03
微信小程序 導(dǎo)入圖標(biāo)實(shí)現(xiàn)過程詳解
這篇文章主要介紹了微信小程序 導(dǎo)入圖標(biāo)實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
JavaScript 模式之工廠模式(Factory)應(yīng)用介紹
工廠模式也是對象創(chuàng)建模式之一,它通常在類或類的靜態(tài)方法中去實(shí)現(xiàn),本文將詳細(xì)介紹JavaScript 工廠模式2012-11-11

