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

查看:15
本文介绍了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/

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 original answer 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
    }
}); 

原答案:

remote 只需要在渲染进程中要求其他模块.在主流程中,您只需直接从 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天全站免登陆