首页 > 其他分享 >vue项目用electron打包成exe,并更新exe版本

vue项目用electron打包成exe,并更新exe版本

时间:2023-09-18 11:37:39浏览次数:32  
标签:exe const app autoUpdater electron vue true mainWindow

用electron-build打包

1、下载electron、electron-builder

npm install electron electron-builder -D

2、配置package.json文件

"start": "electron .",

"app:build": "electron-builder"

 

"build": {
"appId": "com.xx.xx",
"productName": "后台管理系统",
"directories": {
"output": "dist_electron"
},
"extends": null,
"asar": true,
"publish": [
{
"provider": "generic",
"url": "更新目录地址"
}
],
"nsis": {
"oneClick": false,
"allowElevation": true,
"allowToChangeInstallationDirectory": true,
"createDesktopShortcut": true,
"createStartMenuShortcut": true,
"shortcutName": "后台管理系统"
},
"dmg": {
"contents": [
{
"x": 410,
"y": 150,
"type": "link",
"path": "/Applications"
},
{
"x": 130,
"y": 150,
"type": "file"
}
]
},
"mac": {
"icon": "src/resources/icons/icon.icns"
},
"win": {
"icon": "src/resources/icons/icon.ico",
"target": [
{
"target": "nsis",
"arch": [
"ia32"
]
}
]
},
"linux": {
"icon": "src/resources/icons/icon.ico"
}
}

3、在根目录创建main.js文件

// Modules to control application life and create native browser window
const {
app,
BrowserWindow
} = require('electron')

function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
// titleBarStyle: 'hidden', //隐藏标题栏
// frame: false, //无边框窗口
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
webSecurity: false,
// preload: path.join(__dirname, 'preload.js')
}
})

// 隐藏顶部菜单
mainWindow.setMenu(null)

// and load the index.html of the app.
mainWindow.loadFile('./dist/index.html')
// mainWindow.loadURL('http://127.0.0.1:8080')
// mainWindow.loadURL(`file://${__dirname}/dist/index.html`)

mainWindow.maximize()
mainWindow.show()

// Open the DevTools.
mainWindow.webContents.openDevTools()
}

// 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()

app.on('activate', function () {
// 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()
})
})

app.on('ready', () => {
// mainWindow = createMainWindow();
// if (!isDevelopment) updateHandle()
})

// 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', function () {
if (process.platform !== 'darwin') app.quit()
})

注意:

要在package.json文件添加一下代码,否则可能不能正常运行,会报错

"main": "main.js",

用electron-updater更新

需要一个静态服务器来暴露文件,我这里用的时nodejs做了一个临时的静态服务器

1、下载electron-updater

npm install electron-updater -D

2、配置package.json文件(和build里面的appId同级)

"publish": [

{

"provider": "generic",

"url":"http://localhost:3008/"

}

],

3、在主进程里面添加检查更新、下载代码(为了方便,把打包代码也放了进去,但是这里没有下载进度条显示,也没有和渲染进程互动,检测到更新后下载无法显示进度)

// Modules to control application life and create native browser window
const {
app,
BrowserWindow,
protocol,
dialog,
} = require('electron')

const {
autoUpdater
} = require('electron-updater')

// 强制打包状态
// Object.defineProperty(app, 'isPackaged', {
// get() {
// return true;
// }
// })

/**
* Set `__static` path to static files in production
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
*/
if (process.env.NODE_ENV !== 'development') {
global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\');
}

// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([{
scheme: "app",
privileges: {
secure: true,
standard: true
}
}, ]);

// 存在新版本时,默认自动下载更新
autoUpdater.autoDownload = false // 若想通过渲染进程手动触发,需要设置autoDownload为false

// autoUpdater.setFeedURL({
// provider: 'generic',
// url: 'http://localhost:3008/', // 打包文件存放地址
// })

function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
// titleBarStyle: 'hidden', //隐藏标题栏
// frame: false, //无边框窗口
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
webSecurity: false,
// preload: path.join(__dirname, 'preload.js')
}
})

// 隐藏顶部菜单
mainWindow.setMenu(null)

// and load the index.html of the app.
mainWindow.loadFile('./dist/index.html')

mainWindow.maximize()
mainWindow.show()

// 打开F12调试窗口
// mainWindow.webContents.openDevTools()

// 关闭窗口提示框
mainWindow.on("close", (e) => {
e.preventDefault();
dialog.showMessageBox(mainWindow, {
type: "warning",
title: "关闭",
message: "是否要关闭程序",
buttons: ["yes", "no"],
}).then((index) => {
if (index.response === 0) {
app.exit();
}
})
})

// =========================================检测软件更新==========================================
autoUpdater.checkForUpdates();

//检查更新
autoUpdater.on('checking-for-update', () => {})

//没有可用更新
autoUpdater.on('update-not-available', () => {})

//有可用更新
autoUpdater.on('update-available', (info) => {
// autoUpdater.downloadUpdate()
dialog.showMessageBox({
type: 'question',
buttons: ['yes', 'no'],
title: '更新提示',
message: '是否下载最新版本?',
}).then((res) => {
if (res.response === 0) {
// 开始下载
autoUpdater.downloadUpdate()
}
})
})

// 更新出错
autoUpdater.on('error', (err) => {
dialog.showMessageBox({
type: 'error',
buttons: ['yes', 'no'],
title: '更新提示',
message: '更新出错!',
})
})

// 下载进度
autoUpdater.on('download-progress', (progressObj) => {
let log_message = progressObj.percent + '% (' + progressObj.transferred + "/" + progressObj.total + ')'

console.log('下载进度:' + log_message);
})

// //更新下载完成
autoUpdater.on('update-downloaded', (info) => {
autoUpdater.quitAndInstall();
// mainWindow.destroy()
})
}

// 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()

app.on('activate', function () {
// 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()
})
})

app.on('ready', () => {})

// 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', function () {
if (process.platform !== 'darwin') app.quit()
})

nodejs静态服务器代码(用express)

const express = require('express');
const path = require('path');
const app = express();
const cors = require('cors');

const PORT = 3008;

// 启用 CORS 中间件
app.use(cors());

// 处理跨域请求
// app.get('/user', (req, res) => {
// res.send({ name: 'John', age: 30 });
// });

// app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static('public'));

// 如果需要添加路由,可以在这里添加

app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);

}); from:https://blog.csdn.net/m0_65432258/article/details/131102030

标签:exe,const,app,autoUpdater,electron,vue,true,mainWindow
From: https://www.cnblogs.com/wangyb56/p/17711403.html

相关文章

  • vue+openlayers 绘制点
    绘制点使用point在绘制点的位置,使用Circle绘制点的样式疑问:将style放在Feature上就绘制不出来样式? <template><divclass="setting"><divclass="title">设置</div><ul><li><p>边框大小:&......
  • VS2015 QT5.9.4 联合编译报错:提示找不到rc.exe
    解决方案:参考链接1、在C:\ProgramFiles(x86)\WindowsKits\10\bin\10.0.19041.0\x64路径下,找到rc.exe和rcdll.dll两个文件,并复制;2、粘贴到MicrosoftVisualStudio14.0\VC\bin对应目录下;3、重新编译运行程序即可。......
  • vue3项目rem自适应大小如何实现
    ❝rem自适应方案只是页面自适应的解决方案之一,本文主要讲解一下实现过程!本文演示的是Vue3语法!❞rem自适应随着现在互联网设备的日益更新,各大尺寸的屏幕参差不穷导致我们的布局在某些小屏或者大屏上与UI的表现并不一致所以,很多人寻求各种解决方案,我们现在的很多移动端框架都是支持......
  • uniapp中使用vue-i18n实现多语言
    一安装vue-i18nnpmivue-i18n@6二添加相关语言配置 如en.json:{"locale.auto":"System","locale.en":"English","locale.zh-hans":"简体中文","locale.zh-hant":"繁体中文","......
  • Vue js 框下制作登录页面的新方法
    ......
  • Vue mavon-editor 本地加载 – 关闭 CDN
    ​ 转载自Vuemavon-editor本地加载–关闭CDN-前端教程。仅自用。时间2022-03-3121:07:09前言在Vue里面使用Markdown编辑器的选择并不多。mavon-editor大概是GitHub上星星最多的VueMarkdown编辑器了,用起来也比较方便。但是由于mavon-editor默认使用Clo......
  • 如何在Vue组件中定义方法
    在Vue组件中定义方法,你可以按照以下步骤进行:在Vue组件的methods选项中定义方法。methods:{methodName(){//方法的具体逻辑},anotherMethod(){//另一个方法的逻辑}}在上述示例中,使用methods选项来定义了两个方法:methodName和anotherMethod。......
  • 关于vue2的模块级总结
    前阵子在赶一个项目的进度,一直没时间做总结,今日闲来无事,消化一下。背景vue2的项目,面向受众为g端内容1.项目原因,单路由下包含详情&列表两页面。根据v-if跳转,笔者这里用的是动态组件的方式2.同样由于项目原因,使用的模块级vuex,因而在使用时,也有了许多盲点:(如图:)使用createNa......
  • vue3中验证手机号的正则表达式
    在Vue3中,你可以使用正则表达式来验证手机号。以下是一个基本的手机号验证正则表达式示例,可以用于检查中国大陆地区的手机号码:constphoneNumberRegex=/^1[3456789]\d{9}$/;//示例用法constphoneNumber="13812345678";if(phoneNumberRegex.test(phoneNumber)){cons......
  • vue项目中的Tinymce富文本编辑器如何从word中粘贴图片上传到七牛云
    Tinymce富文本编辑器粘贴图片时需要上传到自己的空间中才能被打开。一、首先需要安装引入七牛云npminstallqiniu-jsvarqiniu=require('qiniu-js')//orimport*asqiniufrom'qiniu-js'二、同时引入客户端生成的tokenimport{qiniuTokenCreate}from"@/assets/js/qin......