渲染进程向主进程通信
ipcMain
渲染进程
import { ipcRenderer } from 'electron'
ipcRenderer.send('login') //发送 login
主进程
import { ipcMain } from 'electron'
ipcMain.on('login', (event,) => {
console.log(123)
}
remote
remote
是旧版本是内置的模块,
但是在新版本将被弃用,要用@electron/remote
进行代替
/remote
主进程
require('@electron/remote/main').initialize()
渲染进程使用
// 获取当前窗口属性
const { getCurrentWindow } = require('@electron/remote')
@electron/remote参考
渲染进程
获取不到electron
vue.config.js
module.exports = {
pluginOptions: {
electronBuilder: {
nodeIntegration: true
}
}
}
background.js
设置
new BrowserWindow({
width: 415,
height: 450,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
},
})
主进程向渲染进程通信
主进程
win为自己定义的窗口
.webContents.send('downloadProgress', 1)
渲染进程
import { ipcRenderer } from 'electron'
ipcRenderer.on('downloadProgress', (e,) => {
console.log(arg)
})