【发布时间】:2020-10-25 02:34:23
【问题描述】:
我正在使用带角度的电子构建应用程序。我想要实现的一个功能是在按下按钮后,在整个程序开始之前,我想向用户弹出一个对话框消息框向他显示消息。根据用户是否按下是或否,我将结果从 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);
}
}
【问题讨论】:
-
如何调用组件中发生错误的函数?
-
如果您能添加更多关于错误的信息会很有帮助 - 这里究竟是什么对象未定义?
-
@Rhayene 例如,我会告诉你一个简单的例子:在类下我定义了一个变量,如 stopLoop: boolean = false;现在在 this.IpcService.on('getDialogResault', function (event, arg) 里面,如果我调用这样的变量类我没有这个错误和对话框窗口的功能它工作得很好。
-
请使用新信息编辑您的问题以便更好地阅读
-
由于您为回调函数使用了函数语法,
this可能指的是回调函数 - 而不是此上下文中的类。我调试的第一步是在事件监听器中使用箭头函数来排除这种可能性。如果您可以添加示例,这将很有帮助,该示例通过编辑您的问题将错误重现到您的问题(包括周围的类和类字段)中。这样,当 cmets 被清除时,信息就集中在一个地方,并且不会丢失。