如何将参数从主进程传递到 Electron 中的渲染进程

时间:2023-03-05
本文介绍了如何将参数从主进程传递到 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.

附:: 老实说,我的应用程序是用 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.

推荐答案

根据 atom 源代码,查询字符串方法是一种非常简单的可靠方法,尤其是当我们只需要传递一个唯一的字符串参数时:

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 中的渲染进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:在 Electron 中操作 DOM 下一篇:在 Electron 应用程序之外监听键盘事件和鼠标移动

相关文章

最新文章