加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 02:49:06 所属栏目:百科 来源:网络整理
导读:本文介绍了vue-cli多页面工程实践,分享给大家,具体如下: src目录结构 因为是自定义的设置,src下的目录结构需要固定,约定大于配置嘛。 src目录结构: build中的配置 utils.js 增加: /** globPath 获取泛路径下的特定文件 */ exports.getEntities = funct

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

src目录结构

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

src目录结构:

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;
    };

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

/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] })); }

同时,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')地方修改:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读