我正在尝试使用IPC在我的反应组件和主电子进程之间进行通信。在组件中:
export default class A extends React.Component{
.....
openFile = () => {
console.log('test');
ipcRenderer.send('file-command', 'open');
};
.....
}在main.dev.ts中:
mainWindow.webContents.on('file-command', () => {
console.log('file open request');
});
mainWindow.on('file-command', () => {
console.log('file open request');
});
ipcRenderer.on('file-command', () => {
console.log('file open request');
});都试过了。但这些都不管用。
在我的反应电子应用中,如何正确地使用IPC?我用过这个样板:https://github.com/electron-react-boilerplate/electron-react-boilerplate
发布于 2021-01-19 22:00:32
ipcRenderer是您应该在呈现程序过程中使用的模块(如果没有contextIsolation,可以使用附加到contextIsolation页面的脚本,或者预装其他脚本)。
在主进程中使用的模块称为ipcMain。
const {ipcMain} = require('electron');
ipcMain.on('file-command', () => {
console.log('file open request');
});您还可能希望分别查看invoke/handle中的ipcRenderer和ipcMain。他们使与IPC的工作更加愉快。
https://stackoverflow.com/questions/65789936
复制相似问题