我正在使用预加载和渲染器 js 将数据从 html 传递到服务器。我有一个主窗口,然后打开另一个窗口(添加窗口)。我从添加窗口获取数据并将其传递给服务器。我在服务器上收到数据,但我不知道如何将带有数据的回调从服务器发送到主窗口 html。
在预加载中,我有:
contextBridge.exposeInMainWorld(
'windowControls',
{
add: (data)=> ipcRenderer.send('item:add',data),
received:(data)=> ipcRenderer.send('item:received',data)
在渲染器添加窗口中:
var input = document.getElementById("inputItem").value;
windowControls.add(input)
在 app.js 中:
// Catch item:add
ipcMain.on('item:add',(e,item)=>{
console.log('item',item); // Here I can read item
mainWindow.webContents.on('did-finish-load',()=>{
mainWindow.webContents.send('item:received',item)
});
addWindow.close();
})
我应该在 rendererMain 中写什么来获取数据作为主窗口中的回调?主渲染器在第一次运行时执行,而不是在触发回调时执行(如果我用这些行触发了回调)。