成型vue3项目引入electron通信

4 篇文章 0 订阅

成型vue3项目引入electron通信

1、cnpm i electron --save-dev
安装electron之后,要使用electron里面的ipcRenderer模块,在调用的时候,使用require引入electron会报错,fs.existsSync is not a function或者Uncaught ReferenceError: require is not defined
错误显示是node_modules/electron/index.js文件中引入fs.existsSync语句造成的。

const { ipcRenderer } = require('electron')

查资料得知:
渲染进程属于浏览器端,没有集成Node的环境,所以类似 fs 这样的Node的基础包是不可以使用。
因为没有Node环境,所以这种属于node api的require关键词是不可以使用的。

通过使用window.require代替require来引入electron,因为前者不会被webpack编译,在渲染进程require关键字就是表示node模块的系统。

vue端:js

 var renderer = window.require('electron').ipcRenderer
 renderer.send('import-study', "begin to import study")
 

electron端:

ipcMain.on('import-study',(event,message)=>{
   console.log(message)
   dialog.showOpenDialog({
    title:'选择要上传的文件',//对话框的标题
    buttonLabel: '确认', //确定按钮的自定义标签
    properties: [ 'openDirectory', 'multiSelections'], //打开文件的属性,打开文件还是文件夹,隐藏文件,多选文件
   }).then(res=>{
    if(!res.canceled){
      console.log(res.filePaths)
      event.reply('get-file-path',res.filePaths[0])
    }
   }).catch(err=>{
    console.log(err)
   });
})

vue接收electron返回:

 renderer.on('get-file-path', (event: any, arg: string) => {
                   
                   console.log(arg)
                })
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
下面是在 Vue 项目引入 Electron 的详细方法: 1. 首先,需要在 Vue 项目的根目录下安装 Electron: ``` npm install electron --save-dev ``` 2. 在项目根目录下创建一个 `main` 目录,并在该目录下创建一个 `index.js` 文件,用于启动 Electron: ```javascript const { app, BrowserWindow } = require('electron') function createWindow () { // 创建浏览器窗口 let win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } }) // 加载 Vue 项目的主页面 win.loadURL('http://localhost:8080') // 打开开发者工具 win.webContents.openDevTools() } // Electron 初始化完成后执行的回调函数 app.whenReady().then(() => { createWindow() app.on('activate', function () { if (BrowserWindow.getAllWindows().length === 0) createWindow() }) }) // 当所有窗口都关闭时退出应用 app.on('window-all-closed', function () { if (process.platform !== 'darwin') app.quit() }) ``` 在 `package.json` 文件中添加以下脚本: ```json { "scripts": { "electron": "electron main/index.js" } } ``` 3. 修改 `vue.config.js` 文件,在开发模式下执行 `electron` 命令启动 Electron: ```javascript module.exports = { // ... devServer: { before (app, server) { if (process.env.NODE_ENV === 'development') { const electron = require('child_process').spawn('npm', ['run', 'electron']) electron.stdout.on('data', data => { console.log(data.toString()) }) electron.stderr.on('data', data => { console.error(data.toString()) }) electron.on('close', code => { console.log(`Electron exited with code ${code}`) }) } } } } ``` 4. 在 `public` 目录下创建一个 `electron.js` 文件,用于判断当前是否在 Electron 中运行: ```javascript window.isElectron = navigator.userAgent.toLowerCase().indexOf(' electron/') > -1 ``` 5. 修改 `public/index.html` 文件,根据当前是否在 Electron 中运行来加载不同的脚本: ```html <body> <noscript> <strong>We're sorry but my-app doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> </noscript> <div id="app"></div> <script> if (window.isElectron) { require('./electron.js') } else { document.write('<script src="http://localhost:8080/js/chunk-vendors.js"></script><script src="http://localhost:8080/js/app.js"></script>') } </script> </body> ``` 6. 最后,在命令行中执行以下命令启动 Vue 项目: ``` npm run serve ``` 在浏览器中访问 `http://localhost:8080`,即可看到 Vue 项目的主页面。同时,在命令行中执行以下命令启动 Electron: ``` npm run electron ``` 即可看到 Electron 窗口中加载了 Vue 项目的主页面。 注意:在 Electron 中使用 Vue,需要在 `webPreferences` 中设置 `nodeIntegration` 为 `true`,这样才能在渲染进程中使用 Node.js 模块。但是,这样也会存在一些安全问题,需要注意。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值