【发布时间】:2019-07-25 01:53:54
【问题描述】:
我正在尝试在electron 中创建一个按钮,用于打开dialog 以打开文件。 (在本例中,只需打印其名称)
这是我index.html中的按钮:
<div class="alse-element">
<input id="select-song" type="button" value="select song" onclick="select_song()"/>
</div>
从dialog | Electron,据说在这样的渲染器文件中导入dialog:
const { dialog } = require('electron').remote
console.log(dialog)
这是我的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)
我试过Philip's answer:
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)
const remote = require('electron').remote
const dialog = remote.dialog;
但它也不起作用(同样的错误)
我该如何解决这个问题?
【问题讨论】:
-
你可以试试这个方法吗 var electron = require("electron");变种远程=电子。远程; var dialog = remote.dialog;
-
@DILEEPTHOMAS 我收到此错误:
Uncaught TypeError: Cannot read property 'showOpenDialog' of undefined -
在要求之后可以放一个console.log(dialog)。是未定义还是有一些属性
-
@DILEEPTHOMAS 谢谢你,按照你说的做后,我发现另一个错误并修复它。
-
酷快乐编码。 :)
标签: electron javascript node.js electron