电子-ipcRenderer在异步类中不起作用 [英] Electron - ipcRenderer not working in Async Class
问题描述
module.exports
,以便可以从GUI.js
文件中调用类,并且在我的GUI.js
文件中,我需要该类,并且一切正常.
但是在我的课堂上,我正在尝试执行此操作ipcRenderer.send('message', 'Hello');
我收到此错误:
TypeError: Cannot read property 'send' of undefined
是否可以在我的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');
但是我想在我的课上这样做,那与我的主文件不在同一个文件中.
从Main发送到Renderer的消息应通过发送到特定的 webContents
来完成.这就是event.sender.send('myMSG', 'hello')
起作用的原因,而 ipcRenderer.send
不是.后者按照文档中的说明将从渲染器发送到 Main(而且,由于Error告诉您它是未定义的,因此无法从Main进程进行访问).
如ipcMain
的 docs 中所述,您应该访问要发送到的webContents
并在其上调用send
.
因此,为了更正您的代码,您可以
-
将对主窗口的引用传递到
myClass
并在其上调用send
class myClass { constructor(args) { // ... this.mainWindow = args.win } // ... async Test() { this.mainWindow.webContents.send('myMessage', 'Hello'); } }
-
或
send
到实际聚焦的窗口(BrowserWindow.getFocusedWindow()
)如果适合您的需求class myClass { // ... async Test() { BrowserWindow.getFocusedWindow().webContents.send('myMessage', 'Hello'); } }
i have a class with a Constructor and Async functions.
I have done module.exports
so that i could call my Class from my GUI.js
file and in my GUI.js
file, i have required that class, and everything works fine.
But inside my class, im trying to do this ipcRenderer.send('message', 'Hello');
And im getting this error:
TypeError: Cannot read property 'send' of undefined
is it possible to remote the ipcRenderer in my GUI.js?
Thanks.
i have required the module in my main file, and in my renderer file it sends ipcRenderer.send('startMyClass');
And in my Main file: ipcMain.on('startMyClass', (event, args) => {
const client = new myClass();
client.Start();
})
This is my class/index.js file that is being required in my main file.
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;
EDIT: If i dont require it, and have the whole class in my main file, i can do event.sender.send('myMSG', 'hello');
But i want to do it in my class, that's NOT in the same file as my main.
Sending message from Main to Renderer should be done by sending to a specific webContents
. That's why event.sender.send('myMSG', 'hello')
works, while ipcRenderer.send
not. The latter sends from Renderer to Main as stated in docs (and also, cannot be accessed from Main process as your Error told you it's undefined).
As explainded in ipcMain
's docs you should access the webContents
you want to send to and call send
on that.
So to correct your code you can
Pass a reference to the main window to
myClass
and callsend
on thatclass myClass { constructor(args) { // ... this.mainWindow = args.win } // ... async Test() { this.mainWindow.webContents.send('myMessage', 'Hello'); } }
Or
send
to the actually focused window (BrowserWindow.getFocusedWindow()
) if that fits your needsclass myClass { // ... async Test() { BrowserWindow.getFocusedWindow().webContents.send('myMessage', 'Hello'); } }
这篇关于电子-ipcRenderer在异步类中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!