在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();
});
在主窗口的脚本中,我在click事件处理程序中包含以下内容,商店列表:
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:
- 单击编辑存储
- 从服务器获取商店数据
- 打开新窗口以显示商店数据
或
- 单击编辑商店
- 打开新窗口以显示商店数据
- 从服务器获取商店数据
在后者中,我不确定如何获取ID需要从 storeWindow的
脚本中获取商店。
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屋!