因此,我正在使用Electron框架构建一个应用程序,并尝试在父级的click事件上打开子级窗口时在父级窗口和子级窗口之间传递一些数据。我正在使用ipcMain和ipcRenderer将消息从父渲染器进程传递到主进程,再传递给子渲染器进程。我可以跟踪进入主流程的数据,但是不会将其发送到最终渲染器。我不太清楚为什么,但是我的直觉是,这与从main打开子窗口与通过main通过.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>
我知道这个问题以前曾在这里提出过,我看过其他示例,但到目前为止它们还没有奏效。
最佳答案
除非在窗口选项中设置Window.on('show')
,否则show: false
事件不会触发。
winPodInfo = new BrowserWindow({
show: false
});
winPodInfo.once("show", function() {
winPodInfo.webContents.send("channel", arg);
});
winPodInfo.once("ready-to-show", () => {
winPodInfo.show();
});