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

How to pass parameters from main process to render processes in Electron(如何将参数从主进程传递到 Electron 中的渲染进程)
本文介绍了如何将参数从主进程传递到 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 中的渲染进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)