我使用下面的代码导航到父组件上点击“设备硬件回按钮”。我正在使用电容3.0和设备反向按钮正常工作。实际问题是我无法访问回调函数中的类成员。
下面是代码
export class ConfirmBoxComponent extends DialogContentBase implements OnInit{
constructor(public dialog: DialogRef, public myService : MyService) {
super(dialog);
}
ngOnInit(): void {
this.handleHardwareBackButton();
}
public onCancelAction(): void {
console.log("Cancel confirmed", this.dialog)
myService.closeDialog();// closeDialog not available thru arrow or callback functions
}
handleHardwareBackButton(){
App.addListener('backButton',function(){
this.onCancelAction() //not able to access onCancelMethod within callback
})
}
}问题是,我得到的"this.onCancelAction“不是一种方法。此外,我尝试了下面的代码,但没有用。
handleHardwareBackButton(){
App.addListener('backButton',function(){
this.dialog.close({ actionConfirmed : false }); //here this line doesn't get executed. Also no errors observed
}.bind(this))
}我哪里出问题了吗?请指导我如何在回调函数中访问类成员?
发布于 2022-01-11 06:39:06
试试这样的..。
export class ConfirmBoxComponent extends DialogContentBase implements OnInit {
constructor(public dialog: DialogRef) {
super(dialog);
}
ngOnInit(): void {
this.handleHardwareBackButton();
}
public onCancelAction(): void {
console.log("Cancel confirmed", this.dialog)
this.dialog.close({ actionConfirmed: false });
}
handleHardwareBackButton() {
App.addListener('backButton', () => {
this.onCancelAction() //not able to access onCancelMethod within callback
})
}
}https://stackoverflow.com/questions/70662466
复制相似问题