我有一个带有构造函数和异步函数的类。我已经完成了module.exports,这样我就可以从我的GUI.js文件中调用我的类,在我的GUI.js文件中,我需要这个类,而且一切都很好。
但在我的班上,我试着做这个ipcRenderer.send('message', 'Hello');
我得到了这个错误:
TypeError: Cannot read property 'send' of undefined
在我的ipcRenderer中可以远程访问GUI.js吗?
谢谢。
我已经在我的主文件中要求模块,在我的渲染器文件中它发送ipcRenderer.send('startMyClass');
在我的主文件中:ipcMain.on('startMyClass', (event, args) => { const client = new myClass(); client.Start(); })
这是我的主文件中需要的类/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');
但是我想在我的课堂上做这件事,那不是和我的主课在同一个文件里。
发布于 2018-10-16 08:39:36
从Main发送消息到Renderer应该通过发送到特定的webContents来完成。这就是为什么event.sender.send('myMSG', 'hello')工作,而ipcRenderer.send不工作。后者按照docs中的说明从Renderer发送到Main (而且,由于您的错误告诉您它是未定义的,所以不能从主进程访问它)。
正如ipcMain的文档中所解释的那样,您应该访问要发送的webContents并调用send。
因此,要更正代码,您可以
myClass并对其调用send
类myClass {构造函数(Args){ // .this.mainWindow = args.win } // .异步测试(){ this.mainWindow.webContents.send('myMessage','Hello');}send到实际聚焦窗口(BrowserWindow.getFocusedWindow()),如果这符合您的需要
类myClass { // .异步测试(){ BrowserWindow.getFocusedWindow().webContents.send('myMessage',‘Hello’;}https://stackoverflow.com/questions/52814192
复制相似问题