三步搞定electron-vue 軟件自動(dòng)更新

electron
  1. 安裝依賴npm i electron-updater,package.json配置buildpublish參數(shù)如下:

    image.png

  2. 主進(jìn)程添加代碼main/index.js

import { autoUpdater } from 'electron-updater'

// 檢測(cè)更新,在你想要檢查更新的時(shí)候執(zhí)行,renderer事件觸發(fā)后的操作自行編寫(xiě)
!function updateHandle() {
  let message = {
    error: '檢查更新出錯(cuò)',
    checking: '正在檢查更新……',
    updateAva: '檢測(cè)到新版本,正在下載……',
    updateNotAva: '現(xiàn)在使用的就是最新版本,不用更新',
  };
  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)
  });

  // 更新下載進(jìn)度事件
  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("開(kāi)始更新");
      //some code here to handle event
      autoUpdater.quitAndInstall();
    });

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

  ipcMain.on("checkForUpdate",()=>{
      //執(zhí)行自動(dòng)更新檢查
      autoUpdater.checkForUpdates();
  })
}()

// 通過(guò)main進(jìn)程發(fā)送事件給renderer進(jìn)程,提示更新信息
function sendUpdateMessage(text) {
  mainWindow.webContents.send('message', text)
}
  1. 渲染進(jìn)程添加代碼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文件上傳至服務(wù)器


image.png

引用 electron-vue autoupdater

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 開(kāi)始教程之前,請(qǐng)?jiān)试S我假設(shè)你已經(jīng)有了一個(gè)常用的的編輯器(或者 IDE),系統(tǒng)中也安裝了Node.js 和 npm,...
    程序人生_小龍閱讀 50,206評(píng)論 1 38
  • 我討厭下雨 但現(xiàn)在 我盼望著下雨 一切都灰蒙蒙的 沒(méi)有藍(lán)天 沒(méi)有白云 只有無(wú)邊無(wú)際的黑暗 能把人吞噬的黑暗 在空中...
    檸澈simple閱讀 450評(píng)論 1 2
  • 感賞我今天可以美美的睡個(gè)懶覺(jué),他早早地起床為孩子準(zhǔn)備早餐,還送兒子去學(xué)校。 感賞我今天是個(gè)幸福的人,能夠得到領(lǐng)導(dǎo)和...
    張?jiān)?/span>閱讀 235評(píng)論 0 0
  • 尹建莉的《最美的教育最簡(jiǎn)單》,于我而言,它是我的第一本育兒?jiǎn)⒚山逃龝?shū)。書(shū)中的一些觀點(diǎn),顛覆了我所聽(tīng)過(guò)的育兒方法——...
    艾米要奮進(jìn)閱讀 568評(píng)論 4 14
  • 在《清醒思考的藝術(shù)》一書(shū)中,羅爾夫·多貝里講述了一個(gè)關(guān)于馬克斯·普朗克的故事。 1918年,在獲得諾貝爾物理學(xué)獎(jiǎng)之...
    Jodoo閱讀 1,942評(píng)論 2 18

友情鏈接更多精彩內(nèi)容