【问题标题】:Electron - IPC module not sending data to main from renderer电子 - IPC 模块不从渲染器向主发送数据
【发布时间】: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


【解决方案1】:

所以我认为问题在于,由于我使用的是 VSCode,因此我必须首先设置调试启动选项,以便它将调试会话识别为不是节点会话而是电子会话,这将导致 VScode 识别电子的调试器中的子方法(如app、BrowserWindow等)。如果不这样做,它就不会寻找那些并抛出未定义的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 2018-05-21
    • 2021-04-03
    • 2020-04-03
    • 1970-01-01
    • 2021-06-22
    • 2021-04-02
    • 2023-04-01
    相关资源
    最近更新 更多