HarmonyOS ets中设置监听回调的方法中无法使用this

在ets中不支持监听的callback中调用当前this下的别的方法,报this是个undefined错误

class A{
  b() {}
  callback() {
    this.b();
  }
  Register() {
    on("xxx", this.callback)
  }
  Unregister() {
    off("xxx", this.callback)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
HarmonyOS
2024-12-18 15:59:16
621浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

使用箭头函数 on("xxx", () => this.callback())

分享
微博
QQ
微信
回复
2024-12-18 18:18:44


相关问题
HarmonyOS 网络监听多次
560浏览 • 1回复 待解决
HarmonyOS .ets文件不能使用方法重载
412浏览 • 1回复 待解决
HarmonyOS 点击事件方法
693浏览 • 1回复 待解决
HarmonyOS onAreaChange方法问题
666浏览 • 1回复 待解决