在 electron 中使用 node.js 模块
Using node.js modules in electron
我有以下用例:
我想开发一个桌面应用程序,它将 运行 放在 Raspberry Pi 上。我决定使用 Electron 构建桌面应用程序并使用 Angular 作为前端框架 (1)。
我需要为我的应用程序使用一些 npm 模块,以便与 GPIO 引脚或通过以太网 tcp 通信('net' 节点模块)进行通信。我试图在我的 Angular 应用程序中导入它们(例如从 'net' 导入 {Socket};),但我没有让它们工作。
尝试像这样构建网络套接字时出现以下错误:
let client = new Socket();
未捕获(承诺):类型错误:未定义不是构造函数(正在评估 'new net.Socket()')
这是基本架构中的错误吗,我不能在 angular 前端中 运行 代码,这取决于后端。还是我做错了什么?
我还找到了电容(2),可以调用原生sdks。我尝试实现自己的插件,但出现与上述相同的错误。
感谢您的帮助。
(1) https://angularfirebase.com/lessons/desktop-apps-with-electron-and-angular/
(2) https://capacitor.ionicframework.com/docs/plugins/
看来您对 Electron 有点困惑,即使它不是经典的 client:server
模型,...
先了解Electron:
主进程和渲染进程
The main process is for handling/creating BrowserWindows(Renderer) And
for some communication from one renderer-Window to an other one.
(maybe some other special stuff too)
The renderer is where you really run the most of your app. With node,
you have all you need there.
然后您注意到您需要在 渲染器进程(网页)和主进程之间建立通道。
别担心,这里是 remote
派上用场的地方:
Use main process modules from the renderer process.
The remote module provides a simple way to do inter-process communication (IPC) between the renderer process (web page) and the main process.
...甚至反过来...
Note: For the reverse (access the renderer process from the main
process), you can use webContents.executeJavascript.
所以最后你将能够使用双方的所有魔法。
我有以下用例:
我想开发一个桌面应用程序,它将 运行 放在 Raspberry Pi 上。我决定使用 Electron 构建桌面应用程序并使用 Angular 作为前端框架 (1)。
我需要为我的应用程序使用一些 npm 模块,以便与 GPIO 引脚或通过以太网 tcp 通信('net' 节点模块)进行通信。我试图在我的 Angular 应用程序中导入它们(例如从 'net' 导入 {Socket};),但我没有让它们工作。
尝试像这样构建网络套接字时出现以下错误:
let client = new Socket();
未捕获(承诺):类型错误:未定义不是构造函数(正在评估 'new net.Socket()')
这是基本架构中的错误吗,我不能在 angular 前端中 运行 代码,这取决于后端。还是我做错了什么?
我还找到了电容(2),可以调用原生sdks。我尝试实现自己的插件,但出现与上述相同的错误。
感谢您的帮助。
(1) https://angularfirebase.com/lessons/desktop-apps-with-electron-and-angular/
(2) https://capacitor.ionicframework.com/docs/plugins/
看来您对 Electron 有点困惑,即使它不是经典的 client:server
模型,...
先了解Electron:
主进程和渲染进程
The main process is for handling/creating BrowserWindows(Renderer) And for some communication from one renderer-Window to an other one. (maybe some other special stuff too)
The renderer is where you really run the most of your app. With node, you have all you need there.
然后您注意到您需要在 渲染器进程(网页)和主进程之间建立通道。
别担心,这里是 remote
派上用场的地方:
Use main process modules from the renderer process.
The remote module provides a simple way to do inter-process communication (IPC) between the renderer process (web page) and the main process.
...甚至反过来...
Note: For the reverse (access the renderer process from the main process), you can use webContents.executeJavascript.
所以最后你将能够使用双方的所有魔法。