三步搞定electron-vue 软件自动更新

electron
  1. 安装依赖npm i electron-updater,package.json配置buildpublish参数如下:

    image.png

  2. 主进程添加代码main/index.js

import { autoUpdater } from 'electron-updater'

// 检测更新,在你想要检查更新的时候执行,renderer事件触发后的操作自行编写
!function updateHandle() {
  let message = {
    error: '检查更新出错',
    checking: '正在检查更新……',
    updateAva: '检测到新版本,正在下载……',
    updateNotAva: '现在使用的就是最新版本,不用更新',
  };
  const uploadUrl = "http://61.4.184.177:7799/download/"; // 下载地址,不加后面的**.exe
  autoUpdater.setFeedURL(uploadUrl);
  autoUpdater.on('error', function (error) {
    sendUpdateMessage(message.error)
  });
  autoUpdater.on('checking-for-update', function () {
    sendUpdateMessage(message.checking)
  });
  autoUpdater.on('update-available', function (info) {
    sendUpdateMessage(message.updateAva)
  });
  autoUpdater.on('update-not-available', function (info) {
    sendUpdateMessage(message.updateNotAva)
  });

  // 更新下载进度事件
  autoUpdater.on('download-progress', function (progressObj) {
    mainWindow.webContents.send('downloadProgress', progressObj)
  })
  autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {

    ipcMain.on('isUpdateNow', (e, arg) => {
      console.log(arguments);
      console.log("开始更新");
      //some code here to handle event
      autoUpdater.quitAndInstall();
    });

    mainWindow.webContents.send('isUpdateNow')
  });

  ipcMain.on("checkForUpdate",()=>{
      //执行自动更新检查
      autoUpdater.checkForUpdates();
  })
}()

// 通过main进程发送事件给renderer进程,提示更新信息
function sendUpdateMessage(text) {
  mainWindow.webContents.send('message', text)
}
  1. 渲染进程添加代码App.vue
created(){
    const _this = this
    _this.$electron.ipcRenderer.send("checkForUpdate");
    _this.$electron.ipcRenderer.on("message", (event, text) => {
            console.log(arguments);
            _this.tips = text;
            alert(text)
        });
        _this.$electron.ipcRenderer.on("downloadProgress", (event, progressObj)=> {
            console.log(progressObj);
            _this.downloadPercent = progressObj.percent || 0;
        });
        _this.$electron.ipcRenderer.on("isUpdateNow", () => {
            _this.$electron.ipcRenderer.send("isUpdateNow");
        });
  },
  beforeDestroy(){
    // this.$electron.ipcRenderer.removeAll(["message", "downloadProgress", "isUpdateNow"]);
  }

打包,将生成的exe文件和latest.yml文件上传至服务器


image.png

引用 electron-vue autoupdater

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 开始教程之前,请允许我假设你已经有了一个常用的的编辑器(或者 IDE),系统中也安装了Node.js 和 npm,...
    程序人生_小龙阅读 49,900评论 1 38
  • 我讨厌下雨 但现在 我盼望着下雨 一切都灰蒙蒙的 没有蓝天 没有白云 只有无边无际的黑暗 能把人吞噬的黑暗 在空中...
    柠澈simple阅读 396评论 1 2
  • 感赏我今天可以美美的睡个懒觉,他早早地起床为孩子准备早餐,还送儿子去学校。 感赏我今天是个幸福的人,能够得到领导和...
    张元玲阅读 198评论 0 0
  • 尹建莉的《最美的教育最简单》,于我而言,它是我的第一本育儿启蒙教育书。书中的一些观点,颠覆了我所听过的育儿方法——...
    艾米要奋进阅读 535评论 4 14
  • 在《清醒思考的艺术》一书中,罗尔夫·多贝里讲述了一个关于马克斯·普朗克的故事。 1918年,在获得诺贝尔物理学奖之...
    Jodoo阅读 1,881评论 2 18