从渲染过程中需要电子对话框是未定义的

Requiring electron dialog from render process is undefined(从渲染过程中需要电子对话框是未定义的)

本文介绍了从渲染过程中需要电子对话框是未定义的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用电子,并试图在用户单击按钮时打开文件浏览器.在渲染过程中,我试图像这样包含 elctron.dialog 包.

I am using electron and am trying to open a file browser when a user clicks on button. From the render process I am trying to include the elctron.dialog package like this.

const dialog = require( 'electron' ).dialog;

console.log( dialog );

但是控制台日志的结果是undefined

However the result from the console log is undefined

我绝对确定我正在渲染过程中,所以我不确定为什么这不起作用.该文档表明这是正确的做事方式,但似乎不起作用.

I am absolutely sure I am in the rendering process so I am not sure why this is not working. The documentation suggests that this is the correct way of doing things but it appears to not be working.

这是我的 package.json 文件

{
  "name": "my-app",
  "version": "0.1.0",
  "main": "./main.js",
  "scripts": {
    "start": "electron ."
  },
  "dependencies": {
    "electron": "^0.4.1"
  }
}

这是我的 main.js 文件

    'use strict';

    var app = require( 'app' );
    var BrowserWindow = require( 'browser-window' );
    var ipc = require( 'ipc' );

    var mainWindow = null;

    app.on(
        'ready', function () {
            mainWindow = new BrowserWindow(
                {
                    frame : true,
                    height: 700,
                    width : 500
                }
            );

            mainWindow.loadUrl( 'file://' + __dirname + '/app/index.html' );

            mainWindow.openDevTools();
            mainWindow.on(
                'closed', function () {
                    mainWindow = null;
                }
            );

        }
    );

    ipc.on(
        'close-main-window', function () {
            app.quit();
        }
    );

这是渲染的进程文件

    // Add your index.js code in this file
    var ipc = require( 'ipc' );

    const dialog = require( 'electron' ).dialog;

    console.log( dialog );

这是控制台

这不正确吗?

推荐答案

经过几个小时的研究 其他人 向我指出,这样做的新"方式(2016 年 4 月 15 日)如下.

After a few hours of looking into it someone else pointed out to me that the "new" way (4/15/16) of doing this is the following.

var remote = require('remote');
var dialog = remote.require('dialog');

dialog.showOpenDialog({ 
  properties: [ 'openFile' ] }, function ( filename ) {
    console.log( filename.toString() );
  }
);

您必须要求 remote 然后从远程要求对话框.看起来您不再需要 electron

You must require remote and then from remote require dialog. It looks like you no longer need to require electron

这篇关于从渲染过程中需要电子对话框是未定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从渲染过程中需要电子对话框是未定义的

基础教程推荐