单个Electron createWindow()实例变量

时间:2019-01-22 14:48:43

标签: javascript electron

我创建了一个主要是网络应用程序的Electron应用程序,该应用程序与Electron应用程序之间的集成很小。大多数只是通过带有loadURL的BrowserWindow加载网站。

但是,对于每个产生的CreateWindow(它们具有相同的内容),我需要变量来影响单个窗口,而不是所有窗口。

当前,如果我更改了let变量,则所有窗口的变量都会更改。我不确定如何使变量仅在特定窗口中以特定值更新和可用。

例如如果我在一个窗口中设置了userId = 1,那么当我通过网络应用发送ipc消息时,它也在第二个窗口中进行了更改。我不确定这里最好的逻辑是什么。

1 个答案:

答案 0 :(得分:1)

编辑

如果您打开一个窗口的两个单独的实例(如下所示),则变量应分别位于各自的字段中。

只需在main流程中更改位置

// Create the browser window.
win = new BrowserWindow({ width: 800, height: 600 })

// and load the index.html of the window.
win.loadURL('https://google.com')

// Create the browser window.
win2 = new BrowserWindow({ width: 800, height: 600 })

// and load the index.html of the window.
win2.loadURL('https://youtube.com')

编辑2

因此,根据评论,我认为我为您找到了解决方案。如果每个窗口都需要单独的变量,则可以将其存储在自身的窗口对象中。只需确保在破坏对象之前将它们提取(如果您仍然需要它们):)

// Create the browser windows
let win_0 = new BrowserWindow({ width: 800, height: 600 });
let win_1 = new BrowserWindow({ width: 800, height: 600 });

// Set variables
win_0.UID = 'ASDF-ASDF';
win_1.UID = 'QWER-QWER';

// And now you can just get them like
console.log(win_0.UID, win_1.UID);