【问题标题】:Electron - ipcRenderer error: Cannot read property 'send' of undefinedElectron - ipcRenderer 错误:无法读取未定义的属性“发送”
【发布时间】:2019-03-20 01:44:35
【问题描述】:

我有一个带有构造函数和异步函数的类。我已经完成了 module.exports,以便我可以从我的 GUI.js 文件和我的 GUI.js 文件中调用我的类,我需要该类,并且一切正常。

但在我的课堂上,我试图这样做 ipcRenderer.send('message', 'Hello');

我得到了这个错误:

TypeError: 无法读取未定义的属性“发送”

是否可以在我的 GUI.js 中远程控制 ipcRenderer?

谢谢。

我在我的主文件中需要该模块,并在我的渲染器文件中发送ipcRenderer.send('startMyClass');

在我的主文件中:

ipcMain.on('startMyClass', (event, args) => { const client = new myClass(); client.Start(); })

这是我的主文件中需要的 class/index.js 文件。

const request = require('request-promise');
const cheerio = require('cheerio');
const { ipcRenderer } = require('electron')

class myClass {
  constructor() {
    this._jar = request.jar();
    this._request = request.defaults({ jar: this._jar });
  }

  async Start() {

   await this.Test();


  };

  async Test() {
    ipcRenderer.send('myMessage', 'Hello');
   }

}


module.exports = myClass;

编辑:如果我不需要它,并且在我的主文件中有整个类,我可以做 event.sender.send('myMSG', 'hello');

但我想在我的课堂上做,这与我的主文件不在同一个文件中。

【问题讨论】:

标签: electron ipc


【解决方案1】:

编写如下类:

class myClass {
  constructor() {
    if ((<any>window).require) {
      try { ipc = (<any>window).require('electron').ipcRenderer; } catch (e) { throw e; }
    }
    else { console.warn('App not running inside Electron!'); }

    this._jar = request.jar();
    this._request = request.defaults({ jar: this._jar });
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-12
    • 2021-11-07
    • 2021-02-22
    • 2021-01-02
    • 2022-01-24
    • 1970-01-01
    • 2020-12-19
    相关资源
    最近更新 更多