[英]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?这样的事情是否可能,即以这种方式设置
varInner2
的globalVarName
? 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.我感谢任何想法或解决方案,如果这是一个常识问题,我很抱歉。
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
.相反,我最终使用了更详细的
ipcMain
和ipcRenderer
方法。
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.