详解vue-cli多页面工程实践

本文介绍了vue-cli多页面工程实践,分享给大家,具体如下:

src目录结构

因为是自定义的设置,src下的目录结构需要固定,约定大于配置嘛。

src目录结构:

页面 index/ # index 单页面 index.html main.js # 入口文件 page1/ index.html main.js group/ page2/ index.html main.js @H_404_10@

build中的配置

utils.js 增加:

/**

  • globPath 获取泛路径下的特定文件
    */
    exports.getEntities = function (path) {
    let entities = {};
    glob.sync(path).forEach(function (entity) {
    let moduleName = entity.split('/').slice(-2,-1);
    entities[moduleName] = entity
    });
    // eg: { main: './src/module/index/main.js',test: './src/module/group/test/main.js' }
    return entities;
    };

@H_404_10@

webpack.base.conf.js 修改输入和输出:

获取入口文件 entry: utils.getEntities("./src/modules/**/main.js"),... plugins:[] }; /*** * 生成 /index.html */ let utils = require('./utils') let pages = utils.getEntities("./src/modules/**/index.html"); for (let page in pages) { let filename = "index.html"; if(page!=='index'){ filename = page+"/index.html"; } module.exports.plugins.push(new HtmlWebpackPlugin({ filename: filename,template: pages,inject: true,minify: { removeComments: true,collapseWhitespace: true,removeAttributeQuotes: true // more options: // https://github.com/kangax/html-minifier#options-quick-reference },// necessary to consistently work with multiple chunks via CommonsChunkPlugin chunksSortMode: 'dependency',chunks: ['manifest','vendor',page] })); } @H_404_10@

同时,webpack.dev.conf.js和webpack.prod.conf.js中的HtmlWebpackPlugin删除

这时,访问localhost:8080/和localhost:8080/page1即可看到效果

vue-router history模式下的多页面支持

vue-router history模式需要web server支持,这里演示dev环境下的express支持页面的history模式。

build/dev-server.js 在原来require('connect-history-api-fallback')地方修改

文件也会被rewrite let utils = require("./utils"); let history = require('connect-history-api-fallback'); let pages = utils.getEntities("./src/modules/**/index.html"); let rewrites = []; for(let page in pages){ // match: /page/* or /page rewrites.push({from: new RegExp('\/'+page+'\/|^\/'+page+'$'),to: '/'+page+'/index.html'}) } app.use(history({ rewrites: rewrites })); @H_404_10@

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

问题现象 elmentui的el-tree数据加载问题,导致第一次加载选中当前节点和高亮当前节点没有生效。 解决方...
因为刚打开文件,vscode默认是预览状态,如果编辑过之后,就不会有这个问题。 可以通过双击将预览状态接...
前言 上篇文章我们介绍了国产SM4加密算法的后端java实现方案。没有看过的小伙伴可以看一下这篇文章。 h...
在项目中引入动态路由时报错 写法: 报错: Module build failed (from ./node_modules/_eslint-loader@2...
问题产生 在使用babel编译es6时,遇到报错Uncaught ReferenceError: regeneratorRuntime is not define...
父组件的编写 <a:orgCode=orgCode ></a> 在data里面增加...