Electron 未捕获引用错误:未定义对话框
我试图在中创建一个按钮,打开一个Electron 未捕获引用错误:未定义对话框,electron,javascript,node.js,Electron,Javascript,Node.js,我试图在中创建一个按钮,打开一个对话框来打开一个文件。(在本例中,只需打印其名称) 这是我的index.html中的按钮: <div class="alse-element"> <input id="select-song" type="button" value="select song" onclick="select_song()"/> </div> 这是我的renderer.js: const { dialog } = require(
对话框
来打开一个文件。(在本例中,只需打印其名称)
这是我的index.html
中的按钮:
<div class="alse-element">
<input id="select-song" type="button" value="select song" onclick="select_song()"/>
</div>
这是我的renderer.js
:
const { dialog } = require('electron').remote
function 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
而不是var
,我得到了与上面相同的错误):
我试过: 但它也不起作用(同样的错误)
如何解决此问题?问题是我没有注意到控制台顶部未定义
require
在搜索它之后,我找到了,并将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)
}
)
}
你能用这种方法吗var electron=require(“electron”);var remote=electron.remote;var dialog=remote.dialog@DILEEPTHOMAS我得到这个错误:未捕获类型错误:无法读取未定义的属性“showOpenDialog”
请求后,您可以放置一个console.log(dialog)。它是未定义的还是有一些properties@DILEEPTHOMAS谢谢,按照你说的做了之后,我发现了另一个错误并修复了它。酷快乐编码。:)
var remote = require('remote');
var dialog = remote.require('dialog');
Uncaught TypeError: Cannot read property 'showOpenDialog' of undefined
at select_song (renderer.js:5)
at HTMLInputElement.onclick (index.html:14)
const remote = require('electron').remote
const dialog = remote.dialog;
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)
}
)
}