电子:初始化前无法访问对话框 [英] Electron: Cannot access dialog before initialization

查看:61
本文介绍了电子:初始化前无法访问对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个渲染器文件,其唯一目的是打开一个对话框以从中选择文件.我已经尝试重写了很多次,每次都遇到一个不同的错误.我在做什么错了?

确切代码:

  const {ipcRenderer,shell,remote} = require('electron')const dialog = remote.dialog;函数openFileBrowser(){dialog.showOpenDialog(remote.getCurrentWindow(),{属性:["openFile","multiSelections"]}).then(result => {如果(result.canceled === false){console.log(选定的文件路径:")console.log(result.filePaths)}}).catch(err => {console.log(err)})} 

相关HTML:

 < div id ="button-container">< nav>< ul class ="buttons">< li id ="Open" onclick ="openFileBrowser()">代理</li></ul></nav></div> 

错误代码

  renderer.js:37未捕获的ReferenceError:初始化前无法访问对话框"在openFileBrowser(renderer.js:37)在HTMLLIElement.onclick(proxies.html:16) 

使用电子:"7.1.7"

解决方案

自Electron 6.0.0起,函数 dialog.showMessageBox() dialog.showSaveDialog() 返回Promises,不再使用回调函数.

有同步副本 dialog.showMessageBoxSync() dialog.showOpenDialogSync() dialog.showSaveDialogSync() ..>

查看以下代码示例,这些示例显示了显示打开对话框的异步和同步方式:

异步: dialog.showOpenDialog()

  const remote = require("electron").remoteconst对话框= remote.dialogdialog.showOpenDialog(remote.getCurrentWindow(),{属性:["openFile","multiSelections"]}).then(result => {如果(result.canceled === false){console.log(选定的文件路径:")console.log(result.filePaths)}}).catch(err => {console.log(err)}) 

同步: dialog.showOpenDialogSync()

  const remote = require("electron").remoteconst对话框= remote.dialog让结果= dialog.showOpenDialogSync(remote.getCurrentWindow(),{属性:["openFile","multiSelections"]})if(typeof result ==="object"){console.log(选定的文件路径:")console.log(结果)} 

两个版本都可以选择将BrowserWindow作为第一个元素.如果提供了该对话框,则该对话框显示为模式窗口.

查看电子对话框文档以获取详细的使用信息.

I have a renderer file that has the sole purpose of opening a dialog box to select files from. I have tried rewriting this so many times, and each time I get a different error. What am I doing wrong?

Exact code:

const { ipcRenderer, shell, remote } = require('electron')
const dialog = remote.dialog;

function openFileBrowser() {

    dialog.showOpenDialog(remote.getCurrentWindow(), {
        properties: ["openFile", "multiSelections"]
    }).then(result => {
        if (result.canceled === false) {
            console.log("Selected file paths:")
            console.log(result.filePaths)
        }
    }).catch(err => {
        console.log(err)
    })
}

Related HTML:

        <div id="button-container">
            <nav>
                <ul class="buttons">
                    <li id="Open" onclick="openFileBrowser()">Proxies</li>
                </ul>
            </nav>
        </div>

Error Code

renderer.js:37 Uncaught ReferenceError: Cannot access 'dialog' before initialization
    at openFileBrowser (renderer.js:37)
    at HTMLLIElement.onclick (proxies.html:16)

Using Electron: "7.1.7"

解决方案

Since Electron 6.0.0, the functions dialog.showMessageBox(), dialog.showOpenDialog() and dialog.showSaveDialog() return Promises and no longer take callback functions.

There are synchronous counterparts dialog.showMessageBoxSync(), dialog.showOpenDialogSync() and dialog.showSaveDialogSync().

Check out the following code examples showing the asynchronous and the synchronous way of displaying an open dialog:

Asynchronous: dialog.showOpenDialog()

const remote = require("electron").remote
const dialog = remote.dialog

dialog.showOpenDialog(remote.getCurrentWindow(), {
    properties: ["openFile", "multiSelections"]
}).then(result => {
    if (result.canceled === false) {
        console.log("Selected file paths:")
        console.log(result.filePaths)
    }
}).catch(err => {
    console.log(err)
})

Synchronous: dialog.showOpenDialogSync()

const remote = require("electron").remote
const dialog = remote.dialog

let result = dialog.showOpenDialogSync(remote.getCurrentWindow(), {
    properties: ["openFile", "multiSelections"]
})
if (typeof result === "object") {
    console.log("Selected file paths:")
    console.log(result)
}

Both versions can optionally take a BrowserWindow as the first element. If one is provided, the dialog is shown as a modal window.

Check the Electron dialog documentation for detailed usage information.

这篇关于电子:初始化前无法访问对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆