微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vue-cli实现多页面多路由的示例代码

项目下载地址 vue-cli多页面多路由项目示例 :vue+webpack+vue-router+vuex+mock+axios

Usage

This is a project template for vue-cli.

github上找到某大神的一个基于vue-cli模板的vueAdmin后台管理的模板,根据项目需求改成一个页面多路由的vue项目。

PC端:后台管理页面,单独的页面入口,单独的路由。

移动端:业务展示页面,单独的页面入口,单独的路由。

踩了无数的坑,终于是初见效果了,随后继续优化更新

Install

rush:bash;"> # install dependencies npm install

serve with hot reload at localhost:8088

npm run dev

build for production with minification

npm run build

使用Nginx服务器进行访问,地址如下:

PC端 http://localhost/modules/index.html

移动APP http://localhost/modules/index.html

页面配置

vue2.0版本多页面入口,是由webpack配置来完成的,我的项目文件结构如下

rush:plain;"> webpack |---build |---config |---dist |---route 路由 |---src |---api axios请求 |---assets 资源 |---common 公共js资源目录 |---components组件 |---modules各个模块 |---index index模块 |---views 组件 |---index.html |---index.js |---index.vue |---phone phone模块 |---phone.html |---phone.js |---phone.vue |---phone 组件

modules下为多个页面入口,文件名称保持一致,如:

rush:plain;"> modules |---index |---index.html |---index.js

.vue文件名称任意。

原则上这些文件名称都可以随意定,但由于下面entry入口函数的限定,换成其他名字可以会找不到。如果想要起其他文件名,请相应修改getMultiEntry()函数

until.js

until.js中添加getMultiEntry(),依赖 glob插件,需要提前下载好,until.js开始引入

rush:js;"> //获取多级的入口文件 exports.getMultiEntry = function (globPath) { var entries = {},basename,tmp,pathname;

glob.sync(globPath).forEach(function (entry) {
basename = path.basename(entry,path.extname(entry));
tmp = entry.split('/').splice(-4);

var pathsrc = tmp[0]+'/'+tmp[1];
if( tmp[0] == 'src' ){
pathsrc = tmp[1];
}
//console.log(pathsrc)
pathname = pathsrc + '/' + basename; // 正确输出js和html的路径
entries[pathname] = entry;
//console.log(pathname+'-----------'+entry);
});

return entries;
}

~\build\webpack.base.conf.js

找到entry,添加多入口

rush:plain;"> entry:entries,

运行、编译的时候每一个入口都会对应一个Chunk。 PS:终于明白这个chunk的含义了/(ㄒoㄒ)/~~

~\build\webpack.dev.conf.js

文末添加以下配置:

rush:js;"> var pages = utils.getMultiEntry('./src/'+config.moduleName+'/**/*.html'); for (var pathname in pages) { // 配置生成的html文件,定义路径等 var conf = { filename: pathname + '.html',template: pages[pathname],// 模板路径 chunks: [pathname,'vendors','manifest'],// 每个html引用的js模块 inject: true // js插入位置 }; // 需要生成几个html文件,就配置几个HtmlWebpackPlugin对象 module.exports.plugins.push(new HtmlWebpackPlugin(conf)); }

其中config.moduleName = 'modules'

~\build\webpack.prod.conf.js

rush:js;"> ...

//构建生成页面的HtmlWebpackPlugin配置,主要是循环生成
var pages = utils.getMultiEntry('./src/'+config.moduleName+'/*/.html');
for (var pathname in pages) {
var conf = {
filename: pathname + '.html',// 模板路径
chunks: ['vendor',pathname],// 每个html引用的js模块
inject: true,// js插入位置
hash:true
};

webpackConfig.plugins.push(new HtmlWebpackPlugin(conf));
}
module.exports = webpackConfig

其中config.moduleName = 'modules'

至此,多页面的配置已经完成。访问地址为: index : http://localhost:8088/modules/index.html phone : http://localhost:8088/modules/phone.html

browser Support

Modern browsers and Internet Explorer 10+.

snapshots

imsun; white-space: normal; word-spacing: 0px; text-transform: none; font-weight: normal; color: rgb(0,0); font-style: normal; orphans: 2; widows: 2; letter-spacing: normal; text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px">

imsun; white-space: normal; word-spacing: 0px; text-transform: none; font-weight: normal; color: rgb(0,0); font-style: normal; orphans: 2; widows: 2; letter-spacing: normal; text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px">

imsun; white-space: normal; word-spacing: 0px; text-transform: none; font-weight: normal; color: rgb(0,0); font-style: normal; orphans: 2; widows: 2; letter-spacing: normal; text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px">

License

MIT

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


可以通过min-width属性来设置el-table-column的最小宽度。以下是一个示例: <template> <el-table :data="tableData"> <el-table-column prop="item_no&q
yarn dev,当文件变动后,会自动重启。 yanr start不会自动重启 nodemon会监听文件变动,跟yarn dev和yarn start无关。
ref 用于创建一个对值的响应式引用。这个值可以是原始值(如数字或字符串)或对象。当 ref 的值发生变化时,Vue 会自动更新 DOM 或任何其他使用该 ref 的响应式依赖。 使用示例: import { ref } from 'vue'; const count = ref(0
通过修改 getWK005 函数来实现这一点。这里的 query 参数就是发送 GET 请求时的查询参数。你可以将需要的条件作为 query 对象的属性传递进去。比如,如果你想要按照特定的条件查询信息,你可以在调用 getWK005 函数时传递这些条件。例如: getWK005({ conditio
<el-form-item label="入库类型" prop="mt_type"> <el-select v-model="form.mt_type" filterable placeholder="请选择&q
API 变动 样式类名变化: 一些组件的样式类名有所变动,可能需要更新你的自定义样式。 事件名和属性名变化: 某些组件的事件名和属性名发生了变化,需要检查 Element Plus 文档 以了解详细信息。 使用 setup 函数: 在 Vue 3 中,可以使用 Composition API(如 s
安装和引入方式 Element UI (Vue 2): // main.js import Vue from 'vue'; import ElementUI from 'element-ui'; import 'element-ui/lib/theme-cha
排查400 (Bad Request)和解决这个问题,可以按照以下步骤进行: 检查URL和端点:确保URL http://127.0.0.1:8008/basicApp/BS037HModel/ 是正确的,并且该端点在服务器上存在。 检查请求参数:确认发送请求时的任何参数都是正确的,包括查询参数、请
在 Vue.js 中,<template> 标签是一种特殊的标签,它用于定义组件的模板,但不会直接渲染为 HTML 元素。它的主要用途是在编写组件和使用插槽时提供灵活的模板定义。以下是关于 <template> 标签的一些关键概念和使用示例。 基本用法 组件模板:在单文件组件
el-config-provider是Element Plus库中的一个组件,用于提供全局的配置。它是Element Plus在2.0版本中引入的新组件。 el-config-provider组件的作用是允许你在应用程序中统一配置Element Plus的一些全局属性和样式,这些配置将被应用于所有E