我正在做 Electron 项目。在其中,我看过的每个教程都在<script>
标记中添加了js,我想将js代码与html分开。所以我
...
<script type="text/javascript" src="./../scripts/upload.js" ></script>
...
<input onchange="upload()" id='train-button' type="file">File</input>
....
并在js文件中function upload(){
const electron = require('electron');
const {ipcRenderer} = electron;
if (x.files.length == 0) {
alert("Select one or more files.");
}else{
ipcRenderer.send("saveFile",targetFile,destinationPath)
}
}
现在在这里我得到的错误Uncaught ReferenceError: require is not defined
但是,如果我在外部上传功能之外定义了要求,那么它不会运行该行并说electron is undefined
最佳答案
尝试在BrowserWindow配置中设置nodeIntegration:true
。这样做将在您的js文件中启用require
。
但是,这是非常差的安全性做法。我建议使用security-focused template。
This post还可帮助您进一步了解 Electron 应用程序中有关在渲染器/主进程之间使用IPC的最佳实践。
关于javascript - 在javascript文件中导入包,该文件是html的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63872977/