我的渲染过程中有以下内容:
class Component {
findClosestComponent(c) { /* Do stuff */ }
}
class Item extends Component {
download() {
this.myProperty = 123;
ipcRenderer.send('download-game', this);
ipcRenderer.on('download-complete', (evt, dl) => {
console.log(dl.myProperty);
dl.findClosestComponent(GameCard);
});
}
}
然后,我在我的主流程中有这个:
ipcMain.on('download-game', (evt, dl) => {
/* Download the file the send back the object */
evt.sender.send('download-complete', dl);
});
当我执行 download()
方法时,它将当前对象发送到主进程,然后主进程将其发送回。当我尝试访问该方法时findClosestComponent()
它不再存在。但是,当我尝试访问该属性时,它确实存在并打印出来myProperty
。
如何将对象发送到主进程,然后将其取回并访问方法?
Electron的ipcMain
和ipcRenderer
模块在发送消息之前将消息序列化为JSON,因此,如果您想更好地了解进程之间实际发送的内容,请将消息通过JSON.stringify()
。函数/方法和原型链不会跨越进程边界,如果你想让它工作,你必须实现你自己的序列化/反序列化方案。