在 Electron 中,我的主要进程打开了一个 BrowserWindow.BrowserWindow 会加载一个 html 页面,然后同一个窗口最终会加载另一个 html 页面.
In Electron, I have my main process opening a BrowserWindow. The BrowserWindow loads one html page and then the same window eventually loads another html page.
main.js
var mainWindow;
global.mainState = {
settings: {}
}
mainWindow = createWindow('main', {
width: 1000,
height: 800,
});
if (curState == 'load') {
mainWindow.loadURL(`file://${__dirname}/interface/load.html`, {})
}
if (curState == 'login') {
mainWindow.loadURL(`file://${__dirname}/interface/login.html`, {})
}
加载.html
const remote = require('electron').remote;
var testGlobal = remote.getGlobal('mainState')
testGlobal.settings = 'test value'
testGlobal.settings.inner = 'test value2'
main.js 加载第二个页面(login.html)时,全局变量会被删除/取消引用吗?文档说,如果渲染器进程取消引用全局变量,那么该变量将被 gc'd.当我尝试对此进行测试时,我得到了不一致的结果,我只想从比我更聪明的人那里得到一些解释.
When main.js loads the second page (login.html), will the global variable be deleted/dereferenced? The docs say that if the renderer process dereferences a global variable then the variable will be gc'd. When I try to test this I get inconsistent results and I would just like some explanation from someone more wise than I.
testGlobal 将被垃圾回收,因为站点发生了变化.global.mainState 不会被删除,但是当你调用 testGlobal.settings = 'test value' 时它也不会改变,因为 remote.getGlobal() 只是为您提供 mainState 的副本,而不是参考.
testGlobal will be garbage collected, since the site changes. global.mainState will not be deleted, however it will also not change when you call testGlobal.settings = 'test value', because remote.getGlobal() just gives you a copy of mainState and not a reference.
我建议你使用 ipcMain 和 ipcRenderer 到自己同步全局变量.
I would suggest you use ipcMain and ipcRenderer to sync the global variable yourself.
这篇关于如果渲染器进程关闭,将收集电子全局变量垃圾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 Electron 渲染器进程中创建子窗口时如何修复How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器进程中创建子窗口
mainWindow.loadURL("https://localhost:3000/") 在 EmainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 应用程
Electron webContents executeJavaScript:无法在第二个 loaElectron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:无法在第二个 loadURL 上
如何在angular-cli的组件内使用电子浏览器窗口?how to use electron browser window inside components in angular-cli?(如何在angular-cli的组件内使用电子浏览器窗口?)
ElectronJS - 在 Windows 之间共享 redux 存储?ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之间共享 redux 存储?)
如何在电子应用程序中访问相机/网络摄像头?How to access camera/webcamera inside electron app?(如何在电子应用程序中访问相机/网络摄像头?)