如何在Electron中将参数从主过程传递到渲染过程 [英] How to pass parameters from main process to render processes in Electron
问题描述
我有一个可以打开不同窗口的Electron应用程序。
I have an Electron app that can open different windows.
在应用程序启动时,该应用程序打开一组窗口(加载相同的HTML和JS)文件),但具有用于更改每个窗口显示信息的参数。
On app launch the app open a set of window(s) (that load the same HTML and JS files) but with params to change each window displayed infos.
示例:
app.on('ready', async () => {
...
// open window for stuff 1
win1 = new BrowserWindow({
width: 1024,
height: 728
});
win1.loadURL(`file://${__dirname}/app/app.html?id=1`);
// open window for stuff 2
win2 = new BrowserWindow({
width: 1024,
height: 728
});
win2.loadURL(`file://${__dirname}/app/app.html?id=2`);
显然无法在file://路径中传递参数。
我无法在Electron文档或Internet上的其他地方找到明确的解决方案来将渲染的窗口调整为参数。
Obviously passing params in file:// path doesn't work. I can't find a clear solution in Electron documentation or elsewhere on Internet to condition my rendered window to a param.
我可能可以在窗口准备好后使用IPC通信,但似乎有点太复杂了,直到我只想将变量传递给子视图。
I can probably use IPC communication after window ready but it seems a little bit too complicated until I just want pass a variable to my child view.
P.S。 :老实说,我的应用程序是用React / Redux构建的,我想传递给视图的参数是监听该视图的redux存储键。
P.S. : to be totally honest my application is built with React/Redux and the param I want to pass to view is the redux store key to listen for this view.
推荐答案
根据原子源代码,查询字符串方法是一种非常简单的可靠方法,尤其是当我们只需要传递唯一的字符串参数时:
According atom source code the query string method is a reliable way to do that very simply, especially when we only need to pass a unique string param:
// main process
win1.loadURL(`file://${__dirname}/app/app.html?id=${id}`);
// rendered process
console.log(global.location.search);
https://github.com/electron/electron/issues/6504
这篇关于如何在Electron中将参数从主过程传递到渲染过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!