How can we send messages main process to renderer process in Electron

二次信任 提交于 2021-01-17 19:41:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!