我正在使用具有角度的电子来构建应用程序。我要实现的一项功能是在按下按钮之后,在整个过程开始之前,我想向用户弹出一个对话框,向他显示一条消息。根据用户是否按yes或no,我将结果从main.js返回到component,然后调用或不调用component中存在的适当方法。
问题是角度问题会根据我认为的范围给我一个错误(无法设置未定义的属性)。
这是我的尝试:
组件A:
//method of button
this.IpcService.send('navigateToMessageWindow', null);
this.IpcService.on('getDialogResault', function (event, arg) {
//here is the problem if i call another method it doesnt see it. and gives me an error.
});
Main.js
ipcMain.on('navigateToMessageWindow', (event, arg) => {
const options = {
type: 'question',
buttons: ['Cancel', 'Yes', 'No'],
defaultId: 2,
title: 'Information',
message: 'This procedure will take some minutes to complete. Are you sure you want to continue?',
};
dialog.showMessageBox(null, options).then((data) => {
event.sender.send('getDialogResault', [data.response]);
});
});
IPC服务:
import { Injectable } from '@angular/core';
import { ElectronService } from 'ngx-electron';
@Injectable({
providedIn: 'root'
})
export class IpcService {
constructor(private _electronService: ElectronService) { }
public on(channel: string, listener: Function): void {
this._electronService.ipcRenderer.on(channel, (evt, args) => listener(evt, args));
}
public send(channel: string, ...args): void {
this._electronService.ipcRenderer.send(channel, args);
}
}
答案 0 :(得分:0)
使用上述箭头功能解决了该问题。
this.IpcService.send('navigateToMessageWindow', null);
this.IpcService.on('getDialogResault', (event, arg) => {
});