Eelctron+vue如何配置自动更新--简易配置实现

一、下载安装electron-updater

npm install electron-updater --save

二、配置updater,为了方便我们新建一个update.js这里我把它放在util文件夹下,大家复制粘贴就完事

update.js:

import {
    autoUpdater
} from 'electron-updater'
 
import {
    ipcMain
} from 'electron'
let mainWindow = null;
let canQuit = false;
export function updateHandle(window, feedUrl) {
    mainWindow = window;
    //设置更新包的地址
    autoUpdater.setFeedURL(feedUrl);
    // console.log(autoUpdater.setFeedURL(feedUrl));
    //监听升级失败事件
    autoUpdater.on('error', function (error) {
        sendUpdateMessage({
            cmd: 'error',
            message: error
        })
    });

    //监听开始检测更新事件
    autoUpdater.on('checking-for-update', function (message) {
        sendUpdateMessage({
            cmd: 'checking-for-update',
            message: message
        })
    });
    //监听发现可用更新事件
    autoUpdater.on('update-available', function (message) {
        sendUpdateMessage({
            cmd: 'update-available',
            message: message
        })
    });
    //监听没有可用更新事件
    autoUpdater.on('update-not-available', function (message) {
        sendUpdateMessage({
            cmd: 'update-not-available',
            message: message
        })
    });
 
    // 更新下载进度事件
    autoUpdater.on('download-progress', function (progressObj) {
        sendUpdateMessage({
            cmd: 'download-progress',
            message: progressObj
        })
    });

    autoUpdater.on('close', (event) => {
        if (!canQuit) {
            mainWindow.hide();
            mainWindow.setSkipTaskbar(true);
            event.preventDefault();
        }
    });

    //监听下载完成事件
    autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl) {
        sendUpdateMessage({
            cmd: 'update-downloaded',
            message: {
                releaseNotes,
                releaseName,
                releaseDate,
                updateUrl
            }
        })
        //退出并安装更新包
        if (process.platform !== 'darwin') {
            canQuit = true;
            autoUpdater.quitAndInstall();
        }
        // autoUpdater.quitAndInstall();
    });
 
    //接收渲染进程消息,开始检查更新
    ipcMain.on("checkForUpdate", (e, arg) => {
        //执行自动更新检查
        // sendUpdateMessage({cmd:'checkForUpdate',message:arg})
        if(arg){
            autoUpdater.autoDownload = true;
        }
        autoUpdater.checkForUpdates();
    })
}
//给渲染进程发送消息
function sendUpdateMessage(text) {
    mainWindow.webContents.send('message', text)
}

三、解释

其实electron更新的原理就是使用本地的版本号去比对线上服务器的安装包版本,如果线上的版本比本地的高则会触发更新,那么安装包的版本号在哪里呢?就在package.json中:

{
"version": "0.3.0",
......
}

但是这里配置了版本还不够,我们需要在vue.config.js中配置:

 "publish": [
          {
            "provider": "generic",
            "url": "https://服务器Ip或域名/update/"//比如安装包跟latest.yml放在服务器地址的update文件夹下
          }
        ],

只有配置了这个,在打包的时候才会产生latest.yml这个文件,这个文件就是对你安装包版本的一个描述,在进行版本比对的时候 其实就是在读取这个文件的内容
在vue.config.js中:

 const { defineConfig } = require('@vue/cli-service')
const path = require('path');
function resolve(dir){
    return path.join(__dirname,dir)//path.join(__dirname)设置绝对路径
}
module.exports = defineConfig({
  pluginOptions: {
    electronBuilder:{
      customFileProtocol: "./",
      builderOptions:{
        nsis:{
          allowToChangeInstallationDirectory:true,
          oneClick:false
        },
        asar: false,
        "publish": [
          {
            "provider": "generic",
            "url": "https://ntsfpc.ireql.com/update/"
          }
        ],
      }
    }
  },
  transpileDependencies: true,
  devServer: { // 本地配置
    open: false // 是否自动打开浏览器
  },
})

四、引用update.js并且开启检查更新

1.在background.js中 引入update.js

const {
	updateHandle
} = require('@/utils/update.js') 

...
updateHandle(win, "https://服务器Ip或域名/update/") ;

2.在app.vue中启动检查更新:

 this.$nextTick(()=>{
      window.ipcRenderer.send("checkForUpdate", "参数值");//这里是我在main.js中把ipcRenderer挂载到windows上了
    })
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
配置一个使用 TypeScript、Vue 3 和 Element Plus 的 Vue 项目,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了最新版本的 Vue CLI。如果没有安装,可以运行以下命令进行全局安装: ``` npm install -g @vue/cli ``` 2. 创建一个新的 Vue 项目,使用以下命令: ``` vue create my-project ``` 接下来,你会被要求选择预设配置。选择 "Manually select features"(手动选择特性)并按回车键。 3. 在特性列表中,使用方向键选择 "TypeScript" 并按空格键进行选择。然后按回车键继续。 4. 在下一个屏幕上,使用方向键选择 "Vue 3" 并按空格键进行选择。然后按回车键继续。 5. 在下一个屏幕上,按下方向键将光标移动到 "Babel" 上,并按空格键进行选择。 6. 继续按下方向键将光标移动到 "Linter / Formatter" 上,并按空格键进行选择。 7. 在下一个屏幕上,你可以选择你喜欢的配置文件格式。你可以根据自己的喜好进行选择,然后按回车键继续。 8. 选择 "In dedicated config files"(在独立的配置文件中)并按回车键继续。 9. 这时,Vue CLI 将会安装所需的依赖并创建一个包含 TypeScript 和 Vue 3 的项目。 10. 当安装完成后,进入项目目录: ``` cd my-project ``` 11. 安装 Element Plus 依赖: ``` npm install element-plus ``` 12. 现在,你可以在 Vue 项目中使用 TypeScript 和 Element Plus 来开发了。 希望这能帮助到你开始配置一个 TypeScript、Vue 3 和 Element Plus 的项目!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值