电子:渲染器访问主进程?

电子:渲染器访问主进程?

问题描述:

主进程打开与渲染器要求访问的服务的连接。

The main process opens a connection to a service, to which a renderer requires access.

这可能吗?

我尝试声明 global.thing exports.thing ,并使渲染器 require('electron')。remote.thing —凡事是函数或标量—但没有运气。

I've tried declaring global.thing and exports.thing, and having the renderer require('electron').remote.thing — where thing is a function or scalar — but no luck.

您可以通过更多方式做到这一点:

You can do it in more ways:

1)使用 ipc 在进程之间进行通信,并使用 ipcRenderer.sendSync 询问数据/ strong>函数,该函数询问主进程并等待返回值。
https://github.com/electron /electron/blob/master/docs/api/ipc-renderer.md

2)以正确的方式使用导出,因此:

2) use exports in the right way, so:

在主要过程中:

exports.functionName = functionName;

在渲染器中:

var functionName = remote.require('./main').functionName;