我们如何在Electron中将消息的主要过程发送到渲染器过程 [英] How can we send messages main process to renderer process in Electron
问题描述
我是第一次玩电子游戏.尝试创建文本编辑器
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
})
}
然后 ipcMain
没问题.在菜单上,我有这个:
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)
推荐答案
要将消息发送回渲染器,您将使用:
To send a message back to the renderer you would use:
win.webContents.send('asynchronous-message', {'SAVED': 'File Saved'});
并像这样接收它:
ipcRenderer.on('asynchronous-message', function (evt, message) {
console.log(message); // Returns: {'SAVED': 'File Saved'}
});
其中 asynchronous-message
只是您要将其发送到的频道.它实际上可以是任何东西.
Where asynchronous-message
is simply the channel you're sending it to. It can literally be anything.
这篇关于我们如何在Electron中将消息的主要过程发送到渲染器过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!