【问题标题】:Uncaught ReferenceError: dialog is not defined未捕获的 ReferenceError:未定义对话框
【发布时间】:2019-07-25 01:53:54
【问题描述】:

我正在尝试在 中创建一个按钮,用于打开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 

但它不起作用(同样的错误)


我试过Edgar Martinez's answer

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's answer

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


【解决方案1】:

问题是我没有注意到控制台顶部的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
    }
})

Electron 文档中建议的相同代码有效:

const {dialog} = require('electron').remote

function select_song() {
    dialog.showOpenDialog(
        {properties: ['openFile']}, 
        filename => {
            console.log(filename)
        }
    )
}

【讨论】:

    猜你喜欢
    • 2023-01-23
    • 2016-11-03
    • 2011-01-05
    • 2016-01-02
    • 2013-10-06
    • 2016-12-17
    • 1970-01-01
    相关资源
    最近更新 更多