vue+electron实现更新板块

主程序引入:require('./menu.js')

menu.js

import { app, shell, Menu } from 'electron'
import {checkForUpdates} from './updater.js';
let template = [
    {
        label: '帮助',
        role: 'help',
        submenu: [
            {
                label: `版本` + app.getVersion(),
                id: 'version',
                click: (e) => {
                    checkForUpdates(e)
                }
            },
            {
                label: '学习更多',
                id: 'about',
                click: () => {
                    shell.openExternal('https://baidu.com/')
                }
            }
        ]
    }
]

var list = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(list)

updater.js

import { dialog } from 'electron'
import { autoUpdater } from 'electron-updater'
let updater
autoUpdater.autoDownload = false

autoUpdater.on('error', (error) => {
    dialog.showErrorBox('Error: ', error == null ? "unknown" : (error.stack || error).toString())
})

autoUpdater.on('update-available', (info) => {
    dialog.showMessageBox({
        type: 'info',
        title: '更新提示',
        message: '发现有新版本'+ info.version +',是否更新?',
        buttons: ['更新', '不了'],
        cancelId: 1,
    }).then(index => {
        if (index.response == 0) {
            autoUpdater.downloadUpdate();
        }
        else {
            updater.enabled = true
            updater = null
        }
    })
})

autoUpdater.on('update-not-available', () => {
    dialog.showMessageBox({
        title: '提示',
        message: '暂无更新'
    })
    updater.enabled = false
    updater = null
})

autoUpdater.on('update-downloaded', () => {
    dialog.showMessageBox({
        type: 'info',
        title: '安装更新',
        buttons: ['安装', '稍后安装'],
        message: '安装包已经下载完毕,是否现在安装?',
        cancelId: 1,
    }).then(index => {
        if (index.response == 0) {
            autoUpdater.quitAndInstall()
        }
    })
})



// export this to MenuItem click callback
export function checkForUpdates(menuItem, focusedWindow, event) {
    updater = menuItem
    updater.enabled = false
    autoUpdater.checkForUpdates()
}

vue.config.js中需要设置更新路径,pluginOptions=》electronBuilder=》builderOptions

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue Electron 是一种用于构建跨平台桌面应用程序的开源框架,结合了 Vue.jsElectron.js 技术。在 Vue Electron实现自动更新需要借助 Electron 的 autoUpdater 模块和一些其他辅助工具。 首先,需要在主进程(`main.js`)中配置自动更新。可以使用 Electron 的 autoUpdater 模块来检查新版本,并自动下载和安装更新。具体步骤如下: 1. 导入 autoUpdater 模块:在 `main.js` 文件中添加 `const { autoUpdater } = require('electron-updater')`。 2. 配置更新服务器地址:通过 `autoUpdater.setFeedURL()` 方法设置更新服务器的地址,例如 `autoUpdater.setFeedURL('https://your-update-server-url')`。 3. 监听更新事件:使用 `autoUpdater.on()` 方法监听自动更新过程中的各个事件。例如可以监听 `checking-for-update`、`update-available`、`update-not-available`、`download-progress` 和 `update-downloaded`。 4. 触发检查更新:通过 `autoUpdater.checkForUpdates()` 方法触发检查更新的过程,在应用启动时或者可以通过某个按钮点击事件来手动检查更新。 接下来,需要在渲染进程(Vue 组件)中显示更新提示和进行更新操作。具体步骤如下: 1. 在渲染进程的代码中,可以监听 `message` 事件来接收主进程发送的消息,从而在用户界面上显示更新提示。 2. 监听到更新提示后,可以弹出一个对话框,询问用户是否进行更新。如果用户选择更新,可以通过 `ipcRenderer.send()` 方法向主进程发送消息,触发下载和安装更新的过程。 3. 主进程监听到渲染进程发送的更新请求后,可以通过 `autoUpdater.downloadUpdate()` 方法来下载更新文件。 4. 下载完成后,通过 `autoUpdater.quitAndInstall()` 方法来安装更新并重启应用。 以上就是使用 Vue Electron 实现自动更新的基本步骤。需要注意的是,还需要在应用的打包配置中加入相应的逻辑,以使自动更新在不同平台下运行正常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿木小纸条

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值