从渲染过程中要求电子对话框未定义 [英] Requiring electron dialog from render process is undefined

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

问题描述

我正在使用电子,并试图在用户单击按钮时打开文件浏览器.在渲染过程中,我试图包含这样的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 );

这是控制台

这不正确吗?

推荐答案

经过几个小时的研究

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 ,然后从"remote require"对话框中进行.看来您不再需要电子

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

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

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