我也正在使用鳗鱼构建电子应用程序,尽管我不确定这里是否有用。我最近回到了这个项目,并且我确信它之前已经在工作。除了最小化和关闭按钮(自定义按钮)之外,其他所有功能都起作用。
这是HTML
<div id="title-bar">
<div id="title-bar-btns">
<button id="min-btn" onclick="window_minimise()">—</button>
<button id="close-btn" onclick="window_close()">✕</button>
</div>
</div>
还有JS
function window_minimise(){
const remote = require('electron').remote;
var window = remote.getCurrentWindow();
window.minimize();
}
function window_close(){
const remote = require('electron').remote;
eel.quit_app();
var window = remote.getCurrentWindow();
window.close();
}
还有Python代码
@eel.expose
def quit_app():
sys.exit(0)
我尝试通过http://localhost:8000/html/index.html在浏览器中运行它,当我单击“最小化”时,出现此错误
未捕获的ReferenceError:require未定义 在window_minimise(index.js:111) 在HTMLButtonElement.onclick(index.html:18)
然后关闭
未捕获的ReferenceError:require未定义 在window_close(index.js:117) 在HTMLButtonElement.onclick(index.html:19)
有人知道这里可能出什么问题吗,我该如何解决?
谢谢。
编辑:
这是我的电子main.js文件
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
// 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: 1200, height: 748, icon:'web/images/favicon-32x32.png', resizable: false,
frame: false, show: false, backgroundColor: '#171717'}) // Needs to be changed based on theme
mainWindow.once('ready-to-show', () => {
mainWindow.show()
})
mainWindow.setMenu(null);
// and load the index.html of the app.
mainWindow.loadURL('http://localhost:8000/html/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
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active unti, l the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
其中存在一些错误(缺少分号和let mainWindow
),但是这些错误也存在于电子快速启动的main.js中,所以我很困惑。
答案 0 :(得分:1)
正如我在https://stackoverflow.com/a/56289565/1907797
中所解释的您必须在BrowserWindow webPreferences中将nodeIntegration
设置为true
,因为版本5.0.0的nodeIntegration和webviewTag的默认值为false才能提高安全性。电子相关PR:16235
const mainWindow = new electron.BrowserWindow({
webPreferences: {
nodeIntegration: true
}
});
答案 1 :(得分:0)
请在index.html中启用nodeIntegration,那么只有您可以在渲染器页面中使用require