将 BrowserWindow 重构为单独的文件后捕获异常
Exception caught after BrowserWindow is refactored to a separate file
我已经成功地重构了一些代码来分隔文件(和 类),但 BrowserWindow
除外。 BrowserWindow
处理事情的方式不同吗?我在使用 Electron v.14.
我的 index.js 在重构之前有效:
const electron = require('electron');
const { app, BrowserWindow } = electron;
let mainWindow;
app.on('ready', () => {
mainWindow = new BrowserWindow({
width: 300,
height: 500,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
},
});
mainWindow.loadURL(`file://${__dirname}/src/index.html`);
});
以下是我将 BrowserWindow 重构为单独文件后中断的代码(以及我得到的错误:TypeError: MainWindow is not a constructor
):
index.js:
const electron = require('electron');
const MainWindow = require('./app/main_window');
const { app } = electron
let mainWindow;
app.on('ready', () => {
mainWindow = new MainWindow();
mainWindow.loadURL(`file://${__dirname}/src/index.html`);
});
main_window.js (./app/main_window.js):
const electron = require('electron');
const { BrowserWindow } = electron;
class MainWindow extends BrowserWindow {
constructor() {
super({
width: 300,
height: 500,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
}
});
}
};
您需要导出 class 以将其导入其他文件。
export class MainWindow extends BrowserWindow ....
您可以找到更多信息here。
我已经成功地重构了一些代码来分隔文件(和 类),但 BrowserWindow
除外。 BrowserWindow
处理事情的方式不同吗?我在使用 Electron v.14.
我的 index.js 在重构之前有效:
const electron = require('electron');
const { app, BrowserWindow } = electron;
let mainWindow;
app.on('ready', () => {
mainWindow = new BrowserWindow({
width: 300,
height: 500,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
},
});
mainWindow.loadURL(`file://${__dirname}/src/index.html`);
});
以下是我将 BrowserWindow 重构为单独文件后中断的代码(以及我得到的错误:TypeError: MainWindow is not a constructor
):
index.js:
const electron = require('electron');
const MainWindow = require('./app/main_window');
const { app } = electron
let mainWindow;
app.on('ready', () => {
mainWindow = new MainWindow();
mainWindow.loadURL(`file://${__dirname}/src/index.html`);
});
main_window.js (./app/main_window.js):
const electron = require('electron');
const { BrowserWindow } = electron;
class MainWindow extends BrowserWindow {
constructor() {
super({
width: 300,
height: 500,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
}
});
}
};
您需要导出 class 以将其导入其他文件。
export class MainWindow extends BrowserWindow ....
您可以找到更多信息here。