【问题标题】:How to send data between parent and child window in Electron如何在 Electron 中的父窗口和子窗口之间发送数据
【发布时间】:2019-01-18 06:12:14
【问题描述】:

所以我正在使用 Electron 框架构建一个应用程序,并尝试在父窗口和子窗口之间传递一些数据,因为我在来自父级的单击事件上打开子窗口。我正在使用 ipcMain 和 ipcRenderer 将消息从父渲染器进程传递到主进程,再到子渲染器进程。我可以跟踪进入主进程的数据,但它不会发送到最终渲染器。我不太清楚为什么,但我的直觉是它与从主窗口打开子窗口与从主窗口通过 .webContents.send() 发送数据的事件的协调有关。

相关代码:

将数据从父节点发送到主节点

listItem.click(function(){
    ipcRenderer.send('synchronous-message',feedUrl);
})

在main中监听数据,初始化并打开子窗口,将数据发送到子窗口

let winPodInfo;
ipcMain.on('synchronous-message',(event,arg)=>{

winPodInfo = new BrowserWindow({
   width:500,
   parent:mainWindow,
   modal:true,
   frame:false
});

winPodInfo.loadURL(`file://${__dirname}/podcastInfo.html`);
winPodInfo.once('show',function(){
   winPodInfo.webContents.send('synchronous-message',arg);
})
winPodInfo.once('ready-to-show', () => {
   winPodInfo.show();
});
})

在子渲染器中检查消息

    <script>
    const electron = require('electron');   
    const {ipcRenderer} = electron;

    ipcRenderer.on('synchronous-message',(event,arg)=>{
        console.log(arg);
    })
    </script>

我知道这个问题以前在这里被问过,我也看过其他例子,但到目前为止它们还没有奏效。

【问题讨论】:

    标签: javascript node.js electron


    【解决方案1】:

    在您在窗口选项中设置show: false 之前,Window.on('show') 事件不会触发。

    winPodInfo = new BrowserWindow({
      show: false
    });
    
    winPodInfo.once("show", function() {
      winPodInfo.webContents.send("channel", arg);
    });
    
    winPodInfo.once("ready-to-show", () => {
      winPodInfo.show();
    });
    

    【讨论】:

      猜你喜欢
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      相关资源
      最近更新 更多