我对电子,nodejs和javascript还是很陌生,但是我一直在稳定地研究这些东西以获得不错的结果,但是我在这里遇到了一些麻烦,并且在寻找类似问题方面也没有走运。但是,我确实找到了一个以相同方式使用require的示例,这使我相信这应该可以正常工作。
在电子快速入门中,require('electron')用于加载BrowserWindow类。所以现在我在课堂上尝试做同样的事情。我尝试将模块加载到类的变量中,然后在CreateWindow中使用它。这将导致“ this.BrowserWindow不是构造函数”异常。如果我从Main.js发送模块(该模块以与电子快速入门中完全相同的方式加载模块)作为参数直接传递给我的WindowManager,则它确实起作用。如果没有更好的方法可以做到这一点,我认为应该/应该这样做。
最后,我非常有信心模块确实会以某种方式,形状或形式加载,因为尝试要求无效的模块会给我一个“找不到模块”异常。
所以我的问题是,当我期望与电子快速入门中的行为相同时,为什么会出现异常,以及如何正确/优雅地对其进行修复。
我的课:
class WindowManager {
constructor(CCC) {
this.BrowserWindow = require('electron');
this.ListManager = new (require('./ListManager.js').ListManager)(CCC);
this.ListManager.RequestList("Root");
}
createWindow(BrowserWindow) {
// Create the browser window.
this.mainWindow = new this.BrowserWindow({
height: 600,
width: 800,
webPreferences: {
preload: 'preload.js',
nodeIntegration: true
}
});
// and load the index.html of the app.
this.mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
this.mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
this.mainWindow = null
})
}
}
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
“电子快速入门”示例代码使用ECMAScript 6对象解构语法(为了简洁和方便起见)。
const {app, BrowserWindow} = require('electron')
等效于:
const app = require('electron').app;
const BrowserWindow = require('electron').BrowserWindow;
因此,在class的构造函数中WindowManager
,该行为:
this.BrowserWindow = require('electron');
应该改为:
this.BrowserWindow = require('electron').BrowserWindow;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句