在 Electron 中向 Windows 传递数据 [英] Passing Data to Windows in Electron
问题描述
我正在学习 Electron 并使用多个窗口和 IPC.在我的主脚本中,我有以下内容:
I'm learning Electron and working with multiple windows and IPC. In my main script I have the following:
var storeWindow = new BrowserWindow({
width: 400,
height: 400,
show: false
});
ipc.on('show-store-edit', function(event, store) {
console.log(store);
storeWindow.loadURL('file://' + __dirname + '/app/store.html');
storeWindow.show();
});
在我的主窗口脚本中,我在点击事件处理程序中包含以下内容,拉入商店列表:
And in my primary window's script, I have the following inside of a click event handler, pulling in a list of stores:
$.getJSON("http://localhost:8080/stores/" + item.id).done(function(store) {
ipc.send('show-store-edit', store);
});
在控制台上,我正在从我的服务器打印商店数据.我不清楚的是如何将这些数据放入我的 storeWindow:store.html
的视图中.我什至不确定我是否正确处理了事件序列,但它们会是:
On the console, I am printing the store data from my server. What I'm unclear on is how to get that data into the view for my storeWindow:store.html
. I'm not even sure I'm handling the sequence of events correctly but they would be:
- 点击编辑商店
- 从服务器获取商店数据
- 打开新窗口以显示商店数据
或
- 点击编辑商店
- 打开新窗口以显示商店数据
- 从服务器获取商店数据
在后者中,我不确定如何从 storeWindow 的
脚本中获取获取商店所需的 ID.
In the latter, I'm not sure how I would get the ID required to fetch the store from the storeWindow's
script.
推荐答案
要将事件发送到特定窗口,您可以使用 webContents.send(EVENT_NAME, ARGS)
(参见文档).webContents
是窗口实例的属性:
To send events to particular window you can use webContents.send(EVENT_NAME, ARGS)
(see docs). webContents
is a property of a window instance:
// main process
storeWindow.webContents.send('store-data', store);
要监听这个正在发送的事件,你需要一个窗口进程(渲染器)中的监听器:
To listen for this event being sent, you need a listener in a window process (renderer):
// renderer process
var ipcRenderer = require('electron').ipcRenderer;
ipcRenderer.on('store-data', function (event,store) {
console.log(store);
});
这篇关于在 Electron 中向 Windows 传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!