gpt4 book ai didi

javascript - electron 的 remote.getGlobal() 在 window.location.replace() 之后返回 "undefined"

转载 作者:数据小太阳 更新时间:2023-10-29 06:00:27 24 4
gpt4 key购买 nike

我一直在摆弄 Electron 的远程模块。在我的主进程中,我创建了这个变量:

global.storage = {};

我的渲染器进程是用一个名为 startup.html 的文件初始化的。

win.loadURL('file://' + __dirname + '/startup.html')

在那里,我包含了一个包含以下函数的 javascript 文件:

function enterMain(value){
remote.getGlobal('storage').exmpl = value;
window.location.replace('./general.html');
}

我传递的值是“hello”,当调用...

console.log(remote.getGlobal('storage').exmpl);

...在分配值后,它返回“hello”,这是它应该的。但是,一旦窗口位置被替换为 general.html,我在其中包含一个包含此函数的 javascript 文件:

$(document).ready(function(){
console.log(remote.getGlobal('storage').exmpl);
});

...它返回未定义。为什么?任何人都可以帮助我理解这一点吗?

最佳答案

这里有几件事在起作用:

  • remote 模块在首次访问时将远程对象缓存在渲染器进程中。
  • 在渲染器进程中添加到远程对象的属性不会传播回主进程中的原始对象。
  • 导航重新启动渲染器进程。

考虑到这一点,您的代码中可能会发生以下情况:

  1. remote.getGlobal('storage') 创建一个新的远程对象并将其缓存。
  2. remote.getGlobal('storage').exmpl = value 将新的 exmpl 属性添加到缓存中的远程对象,但不会将其传播到原始对象主进程中的对象。
  3. window.location.replace('./general.html') 重新启动渲染器进程,这会清除远程对象缓存。
  4. console.log(remote.getGlobal('storage').exmpl) 创建一个新的远程对象,因为缓存是空的,但是因为主进程中的原始对象没有exmpl 属性,它在新的远程对象上也是 undefined

remote 模块乍一看看似简单,但它有很多怪癖,其中大部分没有记录,因此将来可能会改变。我建议限制在生产代码中使用 remote 模块。

关于javascript - electron 的 remote.getGlobal() 在 window.location.replace() 之后返回 "undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40202110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com