基于vue-cli搭建一個多頁面應(yīng)用

1.全局安裝vue-cli:Vue.js官方提供的用于快速創(chuàng)建項目模板的腳手架工具
$ npm install -g vue-cli or $ yarn global add vue-cli

2.創(chuàng)建項目模板
$ vue init webpack project-name

3.在安裝過程中會有一些提示:
1)Vue build這個選項選擇Runtime + Compiler
2)安裝vue-router,ESLint、Karma+Mocha、Nightwatch根據(jù)需求選擇安裝

4.現(xiàn)在創(chuàng)建的項目模板是單頁面應(yīng)用,與多頁面應(yīng)用還有些差別,需要做一些調(diào)整:
1)項目目錄結(jié)構(gòu)的調(diào)整:


image.png

在開發(fā)路徑src下增加modules和pages文件夾,分別存放模塊和頁面,
有關(guān)頁面的所有文件都放到同一文件夾下就近管理:index.html(頁面模板)、main.js(頁面入口文件)、App.vue(頁面使用的組件,公用組件放到components文件夾下)、router(頁面的路由配置)、assets(頁面的靜態(tài)資源)都移到index文件夾下,并把main.js改為index.js,保證頁面的入口js文件和模板文件的名稱一致
2)在build/utils.js中添加兩個方法:webpack多入口文件和多頁面輸出

var path = require('path')
var glob = require('glob')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var PAGE_PATH = path.resolve(__dirname, '../src/pages')
var merge = require('webpack-merge')

//多入口配置
exports.entries = function() {
  var entryFiles = glob.sync(PAGE_PATH + '/*/*.js')
  var map = {}
  entryFiles.forEach((filePath) => {
    var filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
    map[filename] = filePath
  })
  return map
}

//多頁面輸出配置
exports.htmlPlugin = function() {
  let entryHtml = glob.sync(PAGE_PATH + '/*/*.html')
  let arr = []
  entryHtml.forEach((filePath) => {
    let filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
    let conf = {
      template: filePath,
      filename: filename + '.html',
      chunks: [filename],
      inject: true
    }
    if (process.env.NODE_ENV === 'production') {
      conf = merge(conf, {
        chunks: ['manifest', 'vendor', filename],
        minify: {
          removeComments: true,
          collapseWhitespace: true,
          removeAttributeQuotes: true
        },
        chunksSortMode: 'dependency'
      })
    }
    arr.push(new HtmlWebpackPlugin(conf))
  })
  return arr
}



3)修改build/webpack.base.conf.js的入口配置

module.exports = {
  entry: utils.entries(),
...



4)修改build/webpack.dev.conf.js和build/webpack.prod.conf.js的多頁面配置:把原有的頁面模板配置注釋或刪除,并把多頁面配置添加到plugins
webpack.dev.conf.js:

  plugins: [
    ......
    //  new HtmlWebpackPlugin({
    //    filename: 'index.html',
    //    template: 'index.html',
    //    inject: true
    //  }),
    ......
  ].concat(utils.htmlPlugin())



webpack.prod.conf.js:

  plugins: [
    ......
    // new HtmlWebpackPlugin({
    //   filename: config.build.index,
    //   template: 'index.html',
    //   inject: true,
    //   minify: {
    //     removeComments: true,
    //     collapseWhitespace: true,
    //     removeAttributeQuotes: true
    //   },
    //   chunksSortMode: 'dependency'
    // }),
    ......
  ].concat(utils.htmlPlugin())



補(bǔ)充說明:在上面多頁面輸出配置中有這樣一行代碼:
chunks: ['manifest', 'vendor', filename],
這是html-webpack-plugin插件對頁面入口文件(即js文件)的限定,如果不設(shè)置則會把整個項目下的所有入口文件全部引入
vendor模塊是指提取涉及node_modules中的公共模塊
manifest模塊是對vendor模塊做的緩存

關(guān)于html-webpack-plugin插件的配置還有一行代碼:
chunksSortMode: 'dependency'
插件會按照模塊的依賴關(guān)系依次加載,即:manifest,vendor,本頁面入口,其他頁面入口...

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容