脚本之家

电脑版
提示:原网页已由神马搜索转码, 内容由www.jb51.net提供.
您的位置:首页网络编程JavaScriptjavascript类库vue.js→ Electron打包成exe与报错解决

vue3使用Electron打包成exe的方法与打包报错解决

  更新时间:2024年06月26日 09:39:41  作者:梦翼横空 
在前端开发中,Electron是一种常用的工具,它允许开发者使用Web技术构建桌面应用程序,本文主要介绍了vue3使用Electron打包成exe的方法与打包报错解决,具有一定的参考价值,感兴趣的可以了解一下

一、安装

1.安装electron

npm install electron --save-dev
npm install electron-builder --save-dev

2.在vue项目根目录新建文件index.js

// main.js
// Modules to control application life and create native browser window
const { app, BrowserWindow,Menu } = require('electron')
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true, // 启用 Node.js 集成
contextIsolation: false, // 禁用上下文隔离(Node.js 集成的一个选项)
webSecurity: false, // 禁用同源策略
contextIsolation: false,
session: {
cookies: true // 这行确保启用了cookie
}
},
icon: 'build/.icon-ico/icon.ico'//这里是自动生成的图标,默认情况下不需要改
})
// and load the index.html of the app.
mainWindow.loadFile('./dist/index.html')//如果要本地化,这样写,如果写远程的,那这里就是请求的域名
//隐藏顶部菜单
// mainWindow.setMenu(null);
// Open the DevTools.
// Open the DevTools.
//mainWindow.webContents.openDevTools()
mainWindow.maximize();//默认最大化
}
//设置中文菜单,默认是英文的
const createMenu = () => {
const template = [
{
label: '文件',
submenu: [
{
label: '退出',
accelerator: 'CmdOrCtrl+Q',
click: () => {
app.quit();
}
}
]
},
{
label: '查看',
submenu: [
{ label: '重新加载', accelerator: 'F5', role: 'reload' },
{ label: '全屏', accelerator: 'F11', role: 'togglefullscreen' },
{ label: '开发者工具', role: 'toggledevtools' }
]
}
];
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
createMenu()//菜单设置
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

3.package.json文件编辑

"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"electron:build": "vue-cli-service build && electron-builder",
"electron:serve": "electron ."
},
"build": {
"productName": "这里是你的项目名",
"appId": "com.example.这里是appId",
"win": {
"icon": "favicon.ico",
"target": ["nsis", "appx"]
},
"directories": {
"output": "build"
},
"files": [
"dist/**/*",
"index.js"//这里是刚才建的index.js
]
},

4.测试

npm run electron:serve

5.打包

npm run electron:build

二、报错解决

 解决:打开cmd 执行 npm config edit

npm config edit

 打开配置文件 粘贴以下内容

registry=https://registry.npm.taobao.org/
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
phantomjs_cdnurl=http://npm.taobao.org/mirrors/phantomjs
electron_mirror=https://npm.taobao.org/mirrors/electron/
ELECTRON_MIRROR=http://npm.taobao.org/mirrors/electron/

三、使用

真正用的时候会出现各种,比例启动exe的图标,cookie。

还有就是,平时我的打包项目不需要指定域名,如果这里用本地化就需要指定域名。

默认配置是不开启cookie的,还有就是用cookie不太好,每次启动exe都需要登录太麻烦了,推荐使用localStorage永久保存。

我的cookie示例,打包exe时使用localStorage,其他情况下使用cookie:

 setCookie(cname, cvalue)
{
if(process.env.NODE_ENV === "host"){
localStorage.setItem(cname, cvalue);
}else{
document.cookie = cname + "=" + cvalue
}
// var d = new Date();
// d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
// var expires = "expires=" + d.toUTCString();
//document.cookie = cname + "=" + cvalue
},
getCookie(cname) {
if(process.env.NODE_ENV === "host"){
return localStorage.getItem(cname);
}else{
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) != -1) {
return c.substring(name.length, c.length);
}
}
return "";
}
},
clearCookie(cname) {
if(process.env.NODE_ENV === "host"){
localStorage.removeItem(cname);
}else{
var d = new Date();
d.setTime(-1);
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=''; " + expires;
}
},

其他代码一并粘出来分享一下。

main.js

const env = process.env.NODE_ENV || 'development';
if(env === "host"){//如果是打包exe需要指定接口域名
axios.defaults.baseURL = process.env.VUE_APP_INTERFACE
axios.defaults.withCredentials = true;//跨域请求的全局凭证
}

package.json

"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"electron:build": "vue-cli-service build --mode host && electron-builder",//使用.env.host配置打包exe
"electron:serve": "electron ."
},
"build": {
"productName": "newsaas",
"appId": "com.example.newsaas",
"win": {
"icon": "./icon256.png",//这里是启动图标必须是256*256的png图
"target": [
"nsis",
"appx"
]
},
"mac": {
"target": "dmg"
},
"nsis": {
"oneClick": true,
"allowToChangeInstallationDirectory": true,
"perMachine": true,
"allowElevation": true,
"createDesktopShortcut": true,
"createStartMenuShortcut": true,
"shortcutName": "index"
},
"directories": {
"output": "build"
},
"files": [
"dist/**/*",
"index.js",
"public/favicon.ico"
]
},

.dev

NODE_ENV=""
VUE_APP_INTERFACE="这里是我的接口域名,只能本地开发时,做代理用,打包不影响"

.dev.host

NODE_ENV="host"
VUE_APP_INTERFACE="http://这里是我的接口域名"

vue.config.js

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
devServer:{
port:8080,
allowedHosts: "all",
webSocketServer:false,
proxy:{
"/":{
target: process.env.VUE_APP_INTERFACE,
changeOrigin:true,
pathRewrite:{
"^/":"/"
}
}
}
},
transpileDependencies: true,
publicPath: './',
// 输出文件目录
assetsDir: 'static',
outputDir: process.env.outputDir,
// eslint-loader 是否在保存的时候检查
lintOnSave: true,

// 生产环境是否生成 sourceMap 文件
productionSourceMap: false
})

以上就是全部代码,如果安装不了electron,或是安装后运行不起来,改一下npm镜像源试试,package-lock.json这个文件记得删除。

到此这篇关于vue3使用Electron打包成exe的方法与打包报错解决的文章就介绍到这了,更多相关Electron打包成exe与报错解决内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

    • 这篇文章主要介绍了Vue如何使用混合Mixins和插件开发详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
      2020-02-02
    • 下面小编就为大家分享一篇vue页面离开后执行函数的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
      2018-03-03
    • 这篇文章主要介绍了Vue 如何import服务器上的js配置文件,帮助大家更好的理解和学习使用vue框架,感兴趣的朋友可以了解下
      2021-04-04
    • 这篇文章主要介绍了Ant Design of Vue的树形控件Tree的使用及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
      2022-10-10
    • 本篇文章主要介绍了详解Nuxt.js Vue服务端渲染摸索,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
      2018-02-02
    • 这篇文章主要给大家介绍了关于多个Vue项目部署到服务器的相关资料,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
      2020-10-10
    • 这篇文章主要介绍了Vue使用z-tree处理大数量的数据以及生成树状结构方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
      2024-04-04
    • uv3-table:一款基于uniapp+vue3跨端自定义手机端增强版表格组件,支持固定表头/列、边框、斑马纹、单选/多选,自定义表头/表体插槽、左右固定列阴影高亮显示,支持编译兼容H5+小程序端+App端,H5+小程序+App端,多端运行一致
      2024-05-05
    • 本文主要介绍了Vite使用unplugin-auto-import实现vue3中的自动导入,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
      2024-06-06
    • 这篇文章主要介绍了Vue 将后台传过来的带html字段的字符串转换为 HTML ,需要的朋友可以参考下
      2018-03-03

    最新评论