简体   繁体   English

ipcRenderer 数据未发送到 Electron 中的 ipcMain

[英]ipcRenderer data not sent to ipcMain in Electron

I recently started developing desktop application using electron.我最近开始使用 electron 开发桌面应用程序。

I want to send form details to main.js from the index.html on button click event.我想在按钮单击事件上从 index.html 将表单详细信息发送到 main.js。 I have added a listener to the button in index.js.我在 index.js 中的按钮上添加了一个监听器。 Searched online and found that I have to use ipcMain in main.js and ipcRenderer in index.js but the data is not being sent to ipcMain.在线搜索,发现我必须在main.js中使用ipcMain和在index.js中使用ipcRenderer ,但数据没有发送到ipcMain。

How do I get the form data in main.js?如何在 main.js 中获取表单数据?

In index.html在索引中。html

<div class="btn-div fld">
         <button id="loginButton" class="btn btn-primary st-btn" type="submit" name="button">Login</button>
</div> 
<script src="index.js" charset="utf-8"></script>

In index.js在 index.js 中

document.querySelector("#loginButton").addEventListener('click', function(){
  userDetails = document.getElementById('login');
  username = userDetails.username.value;
  password = userDetails.password.value;
  console.log("Hello");
  const {ipcRenderer} = require('electron');
  ipcRenderer.send('asynchronous-message', username);
})

In main.js在 main.js

const { app, BrowserWindow , ipcMain } = require('electron')

ipcMain.on('asynchronous-message', (event, arg) => {
  console.log( arg );
});

While creating a browser window in electron using new BrowserWindow(options) where options is an object.在使用new BrowserWindow(options)在 electron 中创建浏览器 window时,其中options是 object。 Define the object as:将 object 定义为:

options = {
    webPreferences: {
         preload: preload.js, //You need to create a file named preload.js (or any name) in your code
         nodeIntegration: true,
         contextIsolation: false,
    }
}

Now in a new file called preload.js :现在在一个名为preload.js的新文件中:

window.ipcRenderer = require('electron').ipcRenderer;

In your snippet you added const { app }... which should be done this way to inject the javascript using a preload property in the object.在您的代码段中,您添加了const { app }...应该以这种方式使用 object 中的preload属性注入 javascript。

Now in the main app.js file (whatever you named maybe index.js ) where you created the browser window:现在在创建浏览器 window 的主app.js文件(无论您命名为index.js的什么)中:

const ipc = require('electron').ipcMain; //Add to your pre-existing code
ipc.on("close-app", (event, message) => { //"close-app" can be anything but, you need to use the same key in the send message side (later in this answer)
    browserWindow.close(); //If you named the browserwindow as browserWindow
});

Now in your HTML (ie, send message side)现在在您的 HTML 中(即发送消息端)

...
<script>
    window.ipcRenderer("close-app", ""); //Second parameter is used if you want to send some extra message. The extra message can be viewed in the server side from the message parameter in the app.js code (just above this paragraph)
</script>

This is a bit difficult if you are doing it for the first time.如果您是第一次这样做,这有点困难。 I've added more articles which will help you clear your confusions:我添加了更多文章,可以帮助您消除困惑:
A related answer at StackOverflow StackOverflow 上的相关答案
Relation with socket.io communication in NodeJS NodeJS 中与 socket.io 通信的关系

While I have seen that the other answer to this question may have worked for others, it did not work for me... I was using webpack and, for the life of me, could not get it to work even when adding ExternalsPlugin commonjs and electron.虽然我已经看到这个问题的另一个答案可能对其他人有用,但它对我不起作用......我正在使用 webpack 并且,在我的一生中,即使添加 ExternalsPlugin commonjs 和electron。 The following worked instead:以下工作改为:

main.js main.js

ipcMain.on("download", (event, info) => {
info.properties.onProgress = status => win.webContents.send("downloadProgress", status);
  });

preload.js preload.js

contextBridge.exposeInMainWorld('electron', {
  api: {
    //receiving message from main.js
    responseProgress: (channel, func) => {
        let validChannels = ["downloadProgress"];
        if (validChannels.includes(channel)) { 
            ipcRenderer.on(channel, (event, ...args) => func(...args));
        }
    },
    process: (channel) => {
    //example for process that is called by button in reactjs etc
    }
});

ReactComponent.js反应组件.js

  function ReactComponent() {
  useEffect(() => {
        //retrieving information from preload.js originally sent from main.js
        window.electron.api.responseProgress("downloadProgress", (progress) => {
        console.log(progress);
        console.log(progress.percent);
        });
          }, []);
  return (
    //example of calling api on button click
    <button onClick={() => {
      window.electron.api.process("toMain");
    }}>Download</button>
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM