【发布时间】:2021-04-12 00:21:22
【问题描述】:
我有一个带有以下代码的打字稿文件 main.dev.ts:
主要开发者.tsclass Main {
mainWindow: BrowserWindow | null = null;
public getMainWindow(): BrowserWindow {
return this.mainWindow!
}
public init() {
app.on('ready', this.createWindow);
}
private async createWindow() {
this.mainWindow = new BrowserWindow({
show: false,
width: 1324,
height: 728,
backgroundColor: "#f0f2f5",
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
}
});
this.mainWindow.loadURL(`file://${__dirname}/app.html`);
this.mainWindow.once('ready-to-show', () => {
setTimeout(() => {
this.mainWindow?.show();
this.mainWindow?.focus();
}, 150);
});
}
}
function createMainApp():Main{
const mainApp = new Main() ;
mainApp.init()
}
export const mainApp = createMainApp()
我有另一个 ts 文件导入这个导出的 mainApp 对象。
import {mainApp} from 'main.dev'
function myFun(){
const window = mainApp.window
//window is "null" even though the main window is already
}
函数myFun 在主窗口创建后(在同一个主进程中)被调用很久。这里的窗口对象是null。
我想当我们在第一个文件中导出对象时,导出对象的所有状态变化都会被保留。我在这里错过了什么?
【问题讨论】:
-
您的意思是
mainWindow?另请注意,调用 init 只是注册一个事件处理程序回调,它不会立即被调用。
标签: javascript node.js typescript electron