当前位置: 首页 > Web前端 > vue.js

electron-vue实战项目(更新..)

时间:2023-03-31 16:39:31 vue.js

开个新坑玩,新项目来不及,应该每周更新,我不说,(≧▽≦)/目前技术栈:electron,vue,elementui、koa主题:未定(金融、书籍)一、环境搭建npminstall-gvue-clivueinitsimulatedgreg/electron-vuemy-projectcdmy-projectnpminstallnpmielement-ui-Snpmrundevnpminstallkoa-statickoa-static-cachekoa-router--saveprojectdirectoryvue代码放在renderer文件夹,koa代码会放在main文件夹然后前端router构建importVuefrom'vue'importRouterfrom'vue-router'constoriginalPush=Router.prototype.pushRouter.prototype.push=functionpush(location){returnoriginalPush.call(this,location).catch(err=>err)}Vue.use(Router)constindexRouter={路径:'/',名称:'索引',component:require('@/components/index').default}constbackUpRouter={path:'*',redirect:'/'}constroutes=[indexRouter,backUpRouter]constrouter=newRouter({模式:'history',base:process.env.VUE_APP_publicPath,routes:routes,scrollBehavior(to,from,savedPosition){return{x:0,y:0}}})router.onError((error)=>{constpattern=/Loadingchunk(\d)+failed/gconstisChunkLoadFailed=error.message.match(pattern)if(isChunkLoadFailed){location.reload()}})exportdefaultrouterkoa中台代码:functioncreateWindow(){constKoa=require('koa')constapp=newKoa()constpath=require('path')conststaticCache=require("koa-static-cache");constserve=require("koa-static")constrouter=require("koa-router")letchild_router=require('./router/index.js')app.use(async(ctx,next)=>{ctx.set('Access-Control-Allow-Origin','*');ctx.set('Access-Control-Allow-Headers','内容类型,内容长度,授权,接受,X-Requested-With,yourHeaderFeild');ctx.set('Access-Control-Allow-Methods','PUT,POST,GET,DELETE,OPTIONS');if(ctx.method=='OPTIONS'){ctx.body=200复制代码;}else{awaitnext();}});app.use(staticCache(path.join(__dirname,"public"),{maxAge:365*24*60*60//将这些文件添加到缓存es一年}))app.use(serve(path.join(__dirname,"public")))router.use('/',child_router);app.use(router.routes()).use(router.allowedMethods());app.listen(2999);mainWindow=newBrowserWindow({height:563,useContentSize:true,width:1000})mainWindow.loadURL(winURL)mainWindow.on('closed',()=>{mainWindow=null})}保持更新中。

猜你喜欢