主进程background.js接收渲染进程的打开要求
const { ipcMain, shell } = require('electron');
ipcMain.on('openDir', async (event, path) => {
console.log('打开路径', path);
try {
const res = await shell.openPath(path);
console.log('ipcMain.on ~ res', res);
} catch (error) {
console.log('ipcMain.on ~ error', error);
}
});
渲染进程发送路径
window?.ipcRenderer?.send('openDir', path);
preload.js
const { contextBridge: bridge, ipcRenderer } = require('electron');
bridge.exposeInMainWorld('ipcRenderer', {
send: (channel, data) => ipcRenderer.send(channel, data),
on: (channel, fun) => ipcRenderer.on(channel, fun),
});
主进程与渲染进程通信可查看另一篇文章