Electron.remote未定义 [英] Electron.remote is undefined

查看:370
本文介绍了Electron.remote未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Electron时遇到麻烦。正如您所看到的标题,当我加载远程模块时,它说它是未定义的。这是条目js的代码:

I have trouble with using Electron. As you can see the title, when i load remote module, it saids it is undefined. This is the code of entry js:

const electron = require('electron');
const { app, BrowserWindow, Tray, remote, ipcMain } = electron;

function initApp() { ... }

app.on('ready', () => {
    initApp();

    console.log(electron);         // object, but no remote inside
    console.log(electron.remote);  // undefined
    console.log(remote);           // undefined
});

,我尝试在此处关注官方文档: http://electron.atom.io/docs/api/remote/

and i tried to follow official doc here: http://electron.atom.io/docs/api/remote/

with

const { remote } = electron;
const { BrowserWindow } = remote;

let win = new BrowserWindow({width: 800, height: 600});  // error! BrowserWindow is not a constructor blabla

...
remote.getCurrentWindow().focus();

我不知道我在想什么。

i don't know what am i missing. any advice will very appreciate.

推荐答案

更新2020 ,因为此答案仍显示在顶部。为了使以上内容能在Electron的当前版本中运行,您需要在主流程中创建窗口时设置 enableRemoteModule

Update 2020, since this answer still appears at the top. For the above to work in current versions of Electron, you need to set enableRemoteModule when creating the window in your main process.

const myWindow = new BrowserWindow({
    webPreferences: {
        enableRemoteModule: true
    }
}); 

旧答案:

远程仅在渲染过程中需要其他模块时才需要。在主要过程中,您只需直接从 require('electron')获取模块。在示例中只是添加了不必要的 remote

remote is needed only to require other modules from inside a render process. In the main process you just get your modules directly from require('electron'). Which it looks like is done in the example just with remote unnecessarily added.

渲染过程:

const { remote } = require('electron');
const { BrowserWindow } = remote;

主要过程:

const { BrowserWindow } = require('electron');

这篇关于Electron.remote未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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