我正在用带有角度的电子制作一个应用程序。我想要实现的一个功能是在按下一个按钮之后,在整个过程开始之前,我想向用户弹出一个对话框消息框,向他显示一条消息。如果用户按了“是”或“否”,我会将结果从main.js返回给组件,并调用或不调用组件中存在的适当方法。
问题是角度给了我一个错误(不能设置属性的未定义),根据我认为的范围。
以下是我的尝试:
组件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);
}
}发布于 2020-07-07 22:05:09
使用上述箭头函数,解决了问题。
this.IpcService.send('navigateToMessageWindow', null);
this.IpcService.on('getDialogResault', (event, arg) => {
});https://stackoverflow.com/questions/62731148
复制相似问题