时间:2013.06.12 发布人:yewan499
已解决问题
谷歌yewan499用户在2013.06.12提交了关于“
决战朝鲜vue 配置多页面应用的示例代码”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2025-02-25T17:56:07。希望大家能够帮助她。
详细问题描述及疑问:期待您的答案,千言万语,表达不了我的感激之情,我已铭记在心
!
希望以下的回答,能够帮助你。
第1个回答
用户名:hamwvlt5687
前言:本文基于vue2.5.2,webpack3.6.0(配置多页面原理类似,实现方法各有千秋,可根据需要进行定制化)
vue是单页面应用。但是在做大型项目时,单页面往往无法满足我们的需求,因此需要配置多页面应用。
1.新建vue项目
vueinit
新液关种webpackvue_multiple_te派好策stcdvue_multiple_testnpmins酸磁裂值早贵兴考陈tall2.安装glob
npmigl九带手绝个英非晶层香ob--s**e-devglob模块用于查找符合要求的文件
杂配析危容3.目标结构目录
.├──***.md├──build│├──build.js│├──check-versions.js│├──logo.png│├──utils.js│├──vue-loader.conf.js│├──webpack.base.conf.js│├──webpack.dev.conf.js│└──webpack.prod.conf.js├──config│├──dev.env.js│├──index.js│└──prod.env.js├──逐协儿***.sh├──index.html├──package-lock.json├──package.json├──src│├──ass型门设神宽史八功ets││└──logo.png│└──pages│├──page1││├──传长抓远增高身宽台叫飞App.vue││├──index.html││└──index.js│└──page2│├──App.vue│├──ind围跑片纸不饭ex.html│└──index.js└──static其中,pages文件夹用于放置页面。page1和page2用于分别放置不同页面,且默认均包含三个文档:A扬击子pp.vue,index.html,index.js,这样在多人协多胞主额英候之率杂似宜作时,可以更为清晰地明确每个文件的含义。除此之外,此文件也可配置路由。
4.utils增加下述代码:
con孙孙委船易长来神片加stglob=require('glob')constPAGE_PATh**=path.resolve(__dirname,'../src/pages')consth**tmlWebpackPlugin=requ用稳ire('html-webpack-plugin')其中:PAGE_PATh**为所有页面所在的文件夹路径,指向pages文件夹。
exports.entries=function(){/*用于匹配pages下一级文件夹中的index亚什守基衡春酸搞所刻.js文件*/varentryFiles=glob.sync(PAGE_PATh**+'/*/index.js')varmap={}entryFiles.forEach((filePath)=>{/*下述两句代码用于取出pages下一级文件夹的名称*/varentryPath=血卫沉款铁path.dirname(filePath)varfilename=entry久掌沙离Path.substring(entryPath.lastIndexOf('\/')+1)老防干成功补够/*生成对应的键衷*/map[filename]=filePath})returnmap}该方法用于生成多页面的入口对象,例如本例,获得的入口对象如下:
{page1:'/Users/work/lea知满rn/vue/vue_multiple_test/src/pages/page1/index.js',page2:'/Users/work/learn/vue/vue_multiple_test/src/pages/page2/index.js',}其中:key为当前页面的文件夹名称,
```value```为当前页面的入口文件名称
exports.htmlPlugin=function(){letentryh**tml=glob.sync(PAGE_PATh**+'/*/index.html')letarr=[]entryh**tml.forEach((filePath)=>{varentryPath=path.dirname(filePath)varfilename=entryPath.substring(entryPath.lastIndexOf('\/')+1)letconf={template:filePath,filename:filename+`/index.html`,chunks:['manifest','vendor',filename],inject:true}if(process.env.NODE_ENV==='pro**ction'){letpro**ctionConfig={minify:{removeComments:true,//移除注释collapseWhitespace:true,//删除空白符和换行符removeAttributeQuotes:true//移除属性引号},chunksSortMode:'dependency'//对引入的chunk模块进行排序}conf={conf,pro**ctionConfig}//合并基础配置和生**环境专属配置}arr.push(newh**tmlWebpackPlugin(conf))})returnarr}4.webpack.base.conf.js修改入口如下:
```entry:utils.entries()```5.webpack.dev.conf.js删除下述代码
newh**tmlWebpackPlugin({filename:'index.html',template:'index.html',inject:true})6.webpack.prod.conf.js删除下述代码
newh**tmlWebpackPlugin({filename:config.build.index,template:'index.html',inject:true,minify:{removeComments:true,collapseWhitespace:true,removeAttributeQuotes:true},chunksSortMode:'dependency'})7.构建结果
【dev】开发环境下,执行npmrundev访问:http://localhost:8080/page1/index.htmlhttp://localhost:8080/page2/index.html即为访问不同的页面【pro**ction】生**环境下,执行npmrunbuild,生成的文件目录如下所示:│├──dist│├──page1││└──index.html│├──page2││└──index.html│└──static│├──css││├──page1.86a4513a3e04c0dcb73e6d6aea4580e4.css││├──page1.***.map││├──page2.86a4513a3e04c0dcb73e6d6aea4580e4.css││└──page2.***.map│└──js│├──manifest.0c1cd46d93b12dcd0191.js│├──manifest.0c1cd46d93b12dcd0191.***.map│├──page1.e2997955f3b0f2090b7a.js│├──page1.***.map│├──page2.4d41f3b684a56847f057.js│├──page2.4d41f3b684a56847f057.***.map│├──vendor.bb335a033c3b9e5d296a.js│└──vendor.***.map8.【懒人福利】使用shell脚本自动构建基础页面
在项目文件下新建shell脚本***.sh,并在脚本中写入下述代码:
#!/bin/bash#打开pages文件夹,并创建文件cdsrc/pagesforfilein$(ls)doif[$file==$1];thenecho$1'文件已存在,请使用其他名字'exitfidonemkdir$1cd$1#生成index.htmlecho"">index.htmlecho'<!DOCTYPEhtml><html><head><metacharset="utf-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title></title></head><body><divid="app"></div><!--builtfileswillbeautoinjected--></body></html>'>index.html#生成App.vueecho"">App.vueecho'<template><divid="app"></div></template><script>exportdefault{name:"App"}</script><style>#app{}</style>'>App.vue#生成index.jsecho"">index.jsecho"importVuefrom'vue'importAppfrom'./App'Vue.config.pro**ctionTip=false/*eslint-disableno-new*/newVue({el:'#app',components:{App},template:'<App/>'})">index.js之后在项目路径下输入下述命令:
bash***.shpage4即可在pages文件夹下生成一个名为page4的新页面。还可以通过自定义shell脚本的内容写入路由等,以实现定制需求。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:用vue构建多页面应用的示例代码