Him*_*aub 5 javascript undefined window.location electron
我一直在摆弄电子的遥控模块.在我的主进程中,我创建了这个变量:
global.storage = {};
Run Code Online (Sandbox Code Playgroud)
我的渲染器进程初始化了一个名为startup.html的文件.
win.loadURL('file://' + __dirname + '/startup.html')
Run Code Online (Sandbox Code Playgroud)
在那里,我包含一个包含以下功能的javascript文件:
function enterMain(value){
remote.getGlobal('storage').exmpl = value;
window.location.replace('./general.html');
}
Run Code Online (Sandbox Code Playgroud)
我传递的价值是"你好",并在呼吁时......
console.log(remote.getGlobal('storage').exmpl);
Run Code Online (Sandbox Code Playgroud)
...在赋值后,它返回"hello",就像它应该的那样.但是,一旦窗口位置被替换为general.html,我在其中包含一个包含此函数的javascript文件:
$(document).ready(function(){
console.log(remote.getGlobal('storage').exmpl);
});
Run Code Online (Sandbox Code Playgroud)
...它返回undefined.为什么?任何人都可以帮我理解这个吗?
这里有一些事情在起作用:
remote模块在首次访问时在渲染器进程中缓存远程对象.考虑到这一点,这可能是您的代码中可能发生的事情:
remote.getGlobal('storage') 创建一个新的远程对象并缓存它.remote.getGlobal('storage').exmpl = valueexmpl向缓存中的远程对象添加新属性,但不将其传播到主进程中的原始对象.window.location.replace('./general.html') 重新启动渲染器进程,该进程会烧掉远程对象缓存.console.log(remote.getGlobal('storage').exmpl)由于缓存为空,因此创建一个新的远程对象,但由于主进程中的原始对象没有exmpl属性,因此它也undefined位于新的远程对象上.该remote模块起初似乎看似简单,但它有许多怪癖,其中大部分都没有记录,因此将来可能会发生变化.我建议remote在生产代码中限制模块的使用.
| 归档时间: |
|
| 查看次数: |
2108 次 |
| 最近记录: |