【发布时间】:2018-10-10 01:53:12
【问题描述】:
我是 Electron 的新手,我正在尝试使用 IPC 模块将一个变量从渲染器进程文件发送到主进程。我在 Devtron 面板中看到 IPC 消息正在从渲染器发送,但它没有显示在 main.js 中已收到任何迹象。我怀疑问题可能与文件如何链接在一起。我从 index.html 中的链接脚本标记调用渲染器文件,但期望渲染器文件(在本例中为 keycapture.js)直接发送到 main.js,我不确定这是如何工作的。
以下是提供和发送 IPC 消息的代码段:
main.js:
app.on('ready',function(){
//Set up a listener for what I've done in keycapture (in the renderer process)
//???
ipc.on('invokeAction', function(event, args){
console.log("RECEIVED IPC IN MAIN!")
var hotkey = args;
console.log(hotkey);
//var result = processData(data);
//event.sender.send('actionReply', result);
//Alright, time to test and troubleshoot.
});
keycapture.js(从 main.js 加载的 index.html 链接):
function keyCancel(ev){
/*******
Use IPC to send the data back to Main to pass on to the local appData file
Modify the below code to fit with what I'm trying to do.
*******/
ipcRenderer.send('invokeAction', hotkey);
//Remove focus from the input field
$(input).blur();
return;
}
【问题讨论】:
-
看起来应该可以了,你能说明你是如何设置ipc的吗?
-
嗯...什么意思?这是 IOC 部分,我需要 IPC 组件在它们各自的文件头中......
-
在 main.js 中不应该是 ipcMain.. 吗?
-
问题似乎是 main.js 早些时候由于无法识别“on”的属性而引发错误。我将为此创建一个新问题。
-
const { ipcMain } = require('electron');在主文件中包含 ipcMain
标签: javascript electron ipc