【发布时间】:2021-05-08 02:52:17
【问题描述】:
我的问题是 - 我有一个 main.js 文件和一个 functions.js 文件,我希望我的一些函数在其中。但是,每当我在该 functions.js 文件中使用 require 时,都会出现错误,require is not defined。
我读过关于其他人有类似问题的帖子,但在他们的情况下,将 nodeIntegration 设置为 true 会有所帮助。这就是我从一开始就拥有的。我知道这个解决方案的问题,但目前我不需要应用程序是安全的,所以如果它有效,我会接受这个解决方案。没有。
我尝试过预加载,但我认为它只是 nodeIntegration 解决方案的“更安全”等价物。作为一个等价物,它也不起作用。你能帮帮我吗?
我的代码:
main.js
const { app, BrowserWindow, ipcMain } = require('electron')
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: true,
preload: 'functions.js'
}
})
win.maximize();
win.loadFile('index.html');
}
app.on('ready', function() {
createWindow();
createOpenDialog();
});
functions.js(从字面上看,这就是代码失败所需要的全部)
const electron = require('electron');
index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="main.css">
<meta charset="UTF-8">
<title>Hello World!</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body style="background: white;">
<script src="functions.js">
</script>
</body>
</html>
【问题讨论】:
标签: javascript html electron require