吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 530|回复: 3
收起左侧

[其他原创] 简单粗暴、踩坑指南:vue内使用electron内的API,如CMD命令、更新等

[复制链接]
Su、 发表于 2024-3-22 16:29
本帖最后由 Su、 于 2024-3-22 16:31 编辑

网上找了很多资料,很多没头没尾的,最终根据多个资料理解整合了下,经过测试没问题,一套下来处理下来,你只要多去翻看electron的使用就可以了愉快的在vue中调用electron的API了
准备工作
[Asm] 纯文本查看 复制代码
#1.创建vue项目
vue create your-project
#2. 
#这里要多一个步骤,不然 add electron-builder的时候可能会报错
npm config edit  #增加三个源
 
#添加下面三个配置
#disturl=https://registry.npmmirror.com/-/binary/node
#electron_mirror=https://registry.npmmirror.com/-/binary/electron/
#registry=https://registry.npmmirror.com
#安装electron-builder打包插件,这里会自动安装electron
vue add electron-builder

image.png
修改background.js找到
[Asm] 纯文本查看 复制代码
    webPreferences: {
 
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
    }
  })
修改为
[Asm] 纯文本查看 复制代码
    webPreferences: {
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      // nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      nodeIntegration: true,//这里修改
      enableRemoteModule: true,//这里添加
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
    }
  })
使用electronAPI方法一App.vue中直接使用
[Asm] 纯文本查看 复制代码
const electron = require('electron')
console.log(electron.remote.app.getName())

图就见方法二的使用中把
方法二新建scr/plugins/VueElectron.js

[Asm] 纯文本查看 复制代码
const electron = require('electron')
 
module.exports = {
  install: function (Vue) {
    Object.defineProperties(Vue.prototype, {
      $electron: {
        get () {
          return electron
        },
      },
    })
  },
}


在main.js中引用VueElectron.js
[Asm] 纯文本查看 复制代码
import VueElectron from '@/plugins/VueElectron'
Vue.use(VueElectron)

image.png
App.vue中使用
[Asm] 纯文本查看 复制代码
export default {
  created () {
    console.log(this.getName())
  },
  methods: {
    getName () {
      return this.$electron.remote.app.getName()
    }
  }
}



效果
c62418b8df62413a87d978b9c343370f (1).png

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

RabbitBearLove 发表于 2024-3-22 16:41
插个眼,学习下
jy262832680 发表于 2024-3-22 16:52
vigo2024 发表于 2024-3-22 18:06
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-30 03:53

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表