首页 > 解决方案 > (ipcMain Eletronjs) 事件未定义

问题描述

最近我一直在开发一个加密文件的 Windows 应用程序(使用 Electronjs)。我想要它,这样当我按下渲染器中的第一个按钮时,它会发送到主进程,要求它打开打开文件对话框。我试过这段代码。

渲染器.js

firstButton.addEventListener("click", openFile)
function openFile() {
    ipc.send('open-the-open-dialogue');
}
ipc.on('file-picked', function(event, arg) {
    console.log(arg);
}

main.js

ipc.on('open-the-open-dialogue',  
    function () {
      var filenames = dialog.showOpenDialogSync({ properties: ['openFile'] });
      if(!filenames) {
        dialog.showErrorBox("ERROR!", "You didn't pick a file to encrypt.")
      }
      event.reply('file-opened', filenames[0]);
    }
);

当我尝试这段代码时,它出现了一个错误,指出未定义事件。那么我做错了什么?

标签: javascriptelectronipc

解决方案


ipcRenderer.on('open-the-open-dialogue',  

    // ipc Listner's callback function should have 2 parameters.

    function (event, args) {
      var filenames = dialog.showOpenDialogSync({ properties: ['openFile'] });

      if(!filenames) {
        dialog.showErrorBox("ERROR!", "You didn't pick a file to encrypt.")
      }

      // You are sending through `file-opened` channel
      // But where is listener? Maybe `file-picked`

      event.reply('file-picked', filenames[0]);
    }
);

推荐阅读