角度&电子-主设备与IPC之间的IPC通信渲染过程 [英] Angular & Electron - IPC communication between main & render processes

查看:82
本文介绍了角度&电子-主设备与IPC之间的IPC通信渲染过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有小模型:

export class SettingsModel {
  DbFileName: string;
}

主要过程:

ipcMain.on('getSettings', function(event) {
    event.sender.send('resultSettings', 'Test settings');
});

IpcService:

IpcService:

import { Injectable } from '@angular/core';
import { ElectronService } from 'ngx-electron';

@Injectable()
export class IpcService {
  constructor(private _electronService: ElectronService) {}

  public on(channel: string, listener: Function): void {
    this._electronService.ipcRenderer.on(channel, listener);
  }

  public send(channel: string, ...args): void {
    this._electronService.ipcRenderer.send(channel, args);
  }
}

最后是角度分量:

export class SettingsComponent {
  constructor(private _electronService: ElectronService, private _db: DbService, private _ipc: IpcService) {
    this.Settings = new SettingsModel();
    console.log("1:" + this.Settings)
    _ipc.send('getSettings');
    console.log("2:" + this.Settings)
    _ipc.on('resultSettings', this._updateSettings);
    console.log("3:" + this.Settings)
  }

  private _updateSettings(evt: any, result: string) {
    console.log("4:" + result);
    console.log("5:" + this.Settings);
    this.Settings.DbFileName = result;
  }

  Settings: SettingsModel;
}

结果铬记录:

1:[object Object]
2:[object Object]
3:[object Object]
4:Test settings
5:undefined
Uncaught TypeError: Cannot set property 'DbFileName' of undefined

似乎IPC可以正常工作,但是由于某种原因,当我收到响应时,我有一个SettingsComponent类的不同实例.我不知道为什么以及如何管理它.有什么想法或建议吗?

It seems that IPC works fine, but for some reason when I get the response I have a different instance of the SettingsComponent class. I don't know why and how to manage it. Any ideas or suggestions?

推荐答案

问题似乎与角度,电子或IPC无关,而与TypeScript有关.

It looks like the problem is related with neither angular nor electon nor IPC, but with TypeScript.

public on(channel: string, listener: Function): void {
  this._electronService.ipcRenderer.on(channel, listener);
}

我必须使用箭头表达式=>:

I have to use arrow expression =>:

public on(channel: string, listener: Function): void {
  this._electronService.ipcRenderer.on(channel, (evt, args) => listener(evt, arg));
}

_ipc.on('resultSettings', (evt, args) => this._updateSettings(evt, args));

这篇关于角度&电子-主设备与IPC之间的IPC通信渲染过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆