问题
I'm playing with electron for the first time. Trying to create a text editor
In render I'm sending a message to indicated the content has changed and needs saving:
document.getElementById('content').onkeyup = e => {
ipcRenderer.send('SAVE_NEEDED', {
content: e.target.innerHTML,
fileDir
})
}
Then ipcMain
receives it no problem. On the menu I have this:
{
label: 'Save',
click: _ => {
saveFile(message)
// trying:
// ipcMain.send('SAVED', 'File Saved')
},
accelerator: 'cmd+S', // shortcut
}
So that the user knows the files has have. But that doesn't seem to work. Is there any other way to do this? I would have thought "save" would be a pre-created role (sort of)
回答1:
To send a message back to the renderer you would use:
win.webContents.send('asynchronous-message', {'SAVED': 'File Saved'});
webContents.send Docs
回答2:
alternatively - when you want to respond to an event received from renderer process you can do something like this:
ipcMain.on("eventFromRenderer", (event) => {
event.sender.send("eventFromMain", someReply);
}
Source: https://electronjs.org/docs/api/ipc-main
来源:https://stackoverflow.com/questions/52124675/how-can-we-send-messages-main-process-to-renderer-process-in-electron