简体   繁体   English

ipcMain 没有收到来自 ipcRenderer 的任何信息

[英]ipcMain receiving nothing from ipcRenderer

I'm trying to do a very simple communication between ipcRenderer and IPCMain but it's not working?我正在尝试在 ipcRenderer 和 IPCMain 之间进行非常简单的通信,但它不起作用? can someone tell me why ?有人可以告诉我为什么吗?

GALLERY.JS画廊.JS

const { ipcRenderer } = require("electron");


document.addEventListener('DOMContentLoaded', (e) => {
    ipcRenderer.send('test');
});

I really don't understand why nothing is printed in my console我真的不明白为什么我的控制台没有打印任何东西

GALERYCONTROLLER.JS画廊控制器.JS

const { ipcMain} = require('electron');
const userId;
const Axios = require('axios')



ipcMain.on('test', (e) =>{
    console.log('droneDataGallery received')
   })

});

gallery.ejs图库.ejs

<link rel="stylesheet" href="../assets/css/GalleryPage.css"></link>

<div class='galleryPage'>

</div>

<script src="./../assets/js/gallery.js"></script>

Thanks a lot for your help !非常感谢你的帮助 !

Assuming you use the latest version of Electron, you need to expose the ipc messaging system to your HTML (gallery.js) code through the contextBrige object at the preload.js level.假设你使用最新版本的 Electron,你需要通过 preload.js 级别的 contextBrige object 将 ipc 消息系统暴露给你的 HTML(gallery.js)代码。 This is very well explained here and it worked great for me. 这在这里得到了很好的解释,对我来说非常有用。 Of course, it implies some extra plumbing.当然,这意味着一些额外的管道。

document.addEventListener('DOMContentLoaded', (e) => {
    ipcRenderer.send(  'test'  ,data you want to send to GALERYCONTROLLER.JS);
});

ipcMain.on('test', (e, data) =>{
// data is array

    console.log(data)
   })

});

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

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