我正在开发一个基于多窗口桌面电子的应用程序,我需要与应用程序的其他窗口共享主应用程序进程中的对象实例。目前在浏览器(Chrome52.0.2743.116)中,您可以参考window.opener来实现这一点。在浏览器中,window.opener (在子窗口中)将为您提供调用window.opent(myURL)的窗口对象的实例。
例如,如果您在window.myData = {}; (Main window)中设置了相同的myData实例,则可以在(子窗口)中使用,并且可以通过window.myData = {}; (Main window)访问
在电子中,当我通过(window.opent(myURL))打开窗口时,window.opener被替换为BrowserWindowProxy (电子API),它不公开调用(window.open(myURL))的窗口(窗口对象实例)。是否有一种在电子中访问window.opener的方式与浏览器中的工作方式相同?
使用remote.sharedObject (Electron )不是一个选项,因为它只序列化/反序列化数据,并且可以用于将数据从一个窗口传递到其他窗口,但不允许跨窗口访问相同的对象实例。
发布于 2018-07-04 09:40:39
更正window.opener,并运行facebook(及其他)登录
您需要使用webPreferences.nativeWindowOpen=true,并为mainWindow设置相同的webPreferences.affinity,并打开窗口(钩子对mainWindow.webContents.on('new-window')) https://gist.github.com/Gvozd/2cec0c8c510a707854e439fb15c561b0
发布于 2016-09-19 08:12:22
我认为你对remote api的看法是不对的。来自文档:
远程模块返回的每个对象(包括函数)表示主进程中的一个对象(我们称之为远程对象或远程函数)。
试试这个:
'use strict'
let Electron = require('electron')
let mainWindow = null
class myObj {
constructor(d) {
console.log('constructor ' + d)
this.d = d
}
get data() {
console.log('get ' + this.d)
return this.d
}
set data(d) {
this.d = d
console.log('set ' + this.d)
}
}
global.someObj = new myObj(1)
Electron.app.on('ready', () => {
mainWindow = new Electron.BrowserWindow()
mainWindow.loadURL(`file://${__dirname}/index.html`)
mainWindow.webContents.openDevTools()
mainWindow.on('closed', () => {
mainWindow = null
})
})
Electron.app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
Electron.app.quit()
}
})和
<!DOCTYPE html>
<html>
<head>
<script>
let obj = require('electron').remote.getGlobal('someObj')
console.log(obj)
console.log(obj.data)
obj.data = 2
console.log(obj.data)
</script>
</head>
<body></body>
</html>https://stackoverflow.com/questions/39474703
复制相似问题