简体   繁体   English

在 Electron 中使用 ipc 从渲染器设置全局变量

[英]Using ipc in Electron to set global variable from renderer

renderer.js渲染器.js

ipcRenderer.sendSync('setGlobal', 'globalVarName').varInner.varInner2 = 'result';

main.js主文件

global.globalVarName = {
  varInner: {
    varInner2: ''
  },
  iWontChange: 'hi'
};

ipcMain.on('setGlobal', (event, arg) => {
  console.log(arg) // should print "result"
  // what goes here?
})

console.log(varInner2) // should print "result"

Is something like this possible, namely setting the varInner2 of globalVarName in this manner?这样的事情是否可能,即以这种方式设置varInner2globalVarName Secondly, is there a way to optimize this so we wouldn't have to rewrite this process for every global variable (ie some way to do this with dynamic variable names)?其次,有没有办法优化这个,这样我们就不必为每个全局变量重写这个过程(即用动态变量名来做这件事的某种方法)?

I appreciate any ideas or solutions, sorry if this is a common sense question.我感谢任何想法或解决方案,如果这是一个常识问题,我很抱歉。

Use IPC to Set the Global's Value.使用 IPC 设置全局值。

Using getGlobal works great when you're only interested in reading the value of the global variable.当您只对读取全局变量的值感兴趣时,使用getGlobal非常有用。 However, I found that trying to assign or change its value using getGlobal to be problematic.但是,我发现尝试使用getGlobal分配或更改其值是有问题的。

In my case, I found that the global variable on the Main process didn't actual change.就我而言,我发现 Main 进程上的全局变量并未实际更改。 Specifically, when refreshing the Electron window in development, the global variables were set back to their original value.具体来说,在开发中刷新 Electron 窗口时,全局变量被设置回其原始值。 This made restoring state in development an issue.这使得在开发中恢复状态成为一个问题。

Not sure if this also was occurring in production, but I imagine it would, so spinning up new processes that relied on up-to-date values of global variables would be problematic.不确定这是否也发生在生产中,但我想它会发生,因此启动依赖于全局变量的最新值的新流程将是有问题的。

Instead, I ended up using the more verbose method of ipcMain and ipcRenderer .相反,我最终使用了更详细的ipcMainipcRenderer方法。

main.js主文件

const { ipcMain } = require( "electron" );

ipcMain.on( "setMyGlobalVariable", ( event, myGlobalVariableValue ) => {
  global.myGlobalVariable = myGlobalVariableValue;
} );

renderer.js渲染器.js

const { ipcRenderer, remote } = require( "electron" );

// Set MyGlobalVariable.
ipcRenderer.send( "setMyGlobalVariable", "Hi There!" );

// Read MyGlobalVariable.
remote.getGlobal( "MyGlobalVariable" ); // => "Hi There!"

Little late to answer but hopefully this will help our future visitors.回答有点晚,但希望这将有助于我们未来的访客。 So, based on the following IPC approach, I was able to create, access and update the value of global variable :因此,基于以下 IPC 方法,我能够创建、访问和更新全局变量的值:

1) Add this code in the main.js file : 1) 在 main.js 文件中添加以下代码:

   global.MyGlobalObject = {
      variable_1: '12345'
   }

2) Use this on your 1st page to update global variable value : 2)在您的第一页上使用它来更新全局变量值:

require('electron').remote.getGlobal('MyGlobalObject').variable_1= '4567'

3) Lastly, use something like this on your 2nd page where you'll access the modified global variable and print it : 3)最后,在您将访问修改后的全局变量并打印它的第二页上使用类似的内容:

console.log(require('electron').remote.getGlobal('MyGlobalObject').variable_1)

You can find the same thing in electron's documentation .您可以在电子文档中找到相同的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Electron 中使用 ipc 从渲染器设置全局变量(再次) - Using ipc in Electron to set global variable from renderer (again) 电子-IPC模块未从渲染器向主机发送数据 - Electron - IPC module not sending data to main from renderer electron 渲染器中的 IPC 因缺少 function __dirname 而引发错误 - IPC in electron renderer throws error from missing function __dirname 从主进程向渲染进程发送 IPC 消息 Electron - Send IPC message from main process to renderer process Electron 如果渲染器进程关闭,则会收集电子全局变量垃圾? - Electron global variable garbage collected if renderer process closes? Electron:使用 IPC、Contextbridge 和 Preload 将变量从 JS 传递到 HTML 的最简单示例? - Electron: Simplest example to pass variable from JS to HTML, using IPC, Contextbridge and Preload? Electron - 在主进程和渲染进程之间设置 IPC 通信 - Electron - Setting up IPC communication between main and renderer processes Electron 渲染器进程:何时应该清理 IPC 侦听器 - Electron Renderer Process: When Should I Clean IPC Listeners 电子ipc使用远程网站? - Electron ipc using remote website? 在 Electron 中,如何通过 preload.js 中定义的全局变量使 ipcRenderer 可用于多个渲染器进程? - In Electron, how to make ipcRenderer available to multiple renderer processes via global variable defined in preload.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM