【问题标题】:Calling node js function from main.ts (electron js and angular js integration)从 main.ts 调用节点 js 函数(电子 js 和角度 js 集成)
【发布时间】:2019-01-29 00:50:41
【问题描述】:

我按照本教程如何集成电子js和角度js。

https://malcoded.com/posts/angular-desktop-electron

我已经运行了它并且运行良好,但现在的问题是我想开始从 angular js 调用 node js 函数,从 main.ts 调用电子 js

例如,当在 Angular js 端按下按钮时,我需要调用节点 js 文件上的函数并发送回响应。

【问题讨论】:

    标签: javascript angular electron


    【解决方案1】:

    对于 Electron 中的通信,您可以使用 ipcMain (docs

    在您的 Electron 代码中,添加类似以下内容的侦听器:

    import { ipcMain } from 'electron';
    ipcMain.on('REQUEST_CHANNEL', (event: any, arg: any) =>
      console.log('received', event, arg);
    );
    

    在您的 Angular 应用程序中,安装模块 ngx-electron (npm i --save ngx-electron),然后向 Electron 发送一条消息,内容如下:

    import { ElectronService } from 'ngx-electron';
    import { IpcRenderer } from 'electron';
    
    // inject the service
    public renderer: IpcRenderer;
    constructor(
        private electronServiceInstance: ElectronService
    ) {
        this.renderer = this.electronServiceInstance.ipcRenderer;
    }
    
    // send the message
    this.renderer.send('REQUEST_CHANNEL', 'my message');
    

    要发回响应,您可以使用多种方式。 我的建议是做相反的事情。 在 Angular 方面,IpcRenderer 监听来自另一个通道的消息,例如 'RESPONSE_CHANNEL',当您希望 Angular 捕获它们时,从 Electron 通过该通道发送消息。

    【讨论】:

    • 教程说 main.ts 使用 typescript 同时我已经在 javascript (node js) 上创建了函数,是否可以将 main.ts 更改为 main.js?
    猜你喜欢
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 2020-01-31
    • 2018-12-03
    • 2018-02-09
    • 1970-01-01
    相关资源
    最近更新 更多