主程序引入: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