且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从反应组件调用节点模块

更新时间:2022-12-16 16:58:24

Node 模块需要从主 Electron 线程运行,而不是运行 React 的渲染器线程.

The Node module needs to be run from the main Electron thread, not the renderer thread on which React runs.

您可以在渲染器进程中运行 NPM 模块,就像在浏览器中一样,但是这些模块不能使用 Node.js 库,因为浏览器中显然没有 Node.

You can run NPM modules in the renderer process, as if you were in the browser, but those modules cannot use the Node.js library since obviously there is no Node in the browser.

要在主(节点)和渲染器(浏览器)线程之间进行通信,您需要使用 IPC(进程间通信)一个使用事件在线程之间发送数据的系统.

To communicate between the main (Node) and renderer (browser) threads you need to use IPC (inter process communication) a system which uses events to send data between threads.

这里是 Electron 的 IPC 文档.

如果您需要线程之间的持续通信,您可以使用 electron-ipc-socket 图书馆.

If you need constant communication between threads you can use the electron-ipc-socket library.