我正在尝试创建一个按钮 电子打开一个dialog打开一个文件。(在本例中,只需打印其名称)这是我的按钮index.html: <div class="alse-element"> <input id="select-song" type="button" value="select song" onclick="select_song()"/> </div>从对话框 | Electron,据说dialog在渲染器文件中导入如下:const { dialog } = require('electron').remoteconsole.log(dialog)所以这是我的renderer.js:const { dialog } = require('electron').remotefunction select_song() { dialog.showOpenDialog( {properties: ['openFile']}, filename => { console.log(filename) } )}但是,当我按下按钮时,此消息会打印在控制台中:Uncaught ReferenceError: dialog is not defined at select_song (renderer.js:4) at HTMLInputElement.onclick (index.html:14)我试过菲利普的回答:const dialog = require('electron').remote.dialog 但它没有用(同样的错误)我试过埃德加马丁内斯的回答:var remote = require('remote');var dialog = remote.require('dialog');但是我收到此错误(如果使用const而不是var,我会收到与上述相同的错误):Uncaught TypeError: Cannot read property 'showOpenDialog' of undefined at select_song (renderer.js:5) at HTMLInputElement.onclick (index.html:14)我试过D.Richard 的回答:const remote = require('electron').remote const dialog = remote.dialog;但它也不起作用(同样的错误)我该如何解决这个问题?
1 回答
catspeake
TA贡献1111条经验 获得超0个赞
问题是我没有注意到require is not defined
控制台顶部的 。
搜索后,我找到了Sathiraumesh's answer,并在添加webPreferences: {nodeIntegration: true}到main.js文件后:
main_window = new BrowserWindow({
width: 800,
height: 600,
show: false,
webPreferences: {
nodeIntegration: true
}
})
电子文档中建议的相同代码有效:
const {dialog} = require('electron').remote
function select_song() {
dialog.showOpenDialog(
{properties: ['openFile']},
filename => {
console.log(filename)
}
)
}
添加回答
举报
0/150
提交
取消