如何实现在同一个UIAbility实例传递事件

如何实现在同一个UIAbility实例传递事件

HarmonyOS
2024-07-30 11:06:30
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
莫莫莫小贝

在UIAbility的框架内,EventHub模块作为事件中心,具备严谨的事件管理能力,包括事件的订阅、取消订阅以及触发。通过EventHub,开发者能够有序地接收和响应各类事件

import { UIAbility } from '@kit.AbilityKit';

export default class EntryAbility extends UIAbility {
  onForeground() {
    this.context.eventHub.on('myEvent', this.eventFunc);

结果:

// eventFunc is called,undefined,undefined
    this.context.eventHub.emit('myEvent');

结果:

// eventFunc is called,1,undefined
    this.context.eventHub.emit('myEvent', 1);

结果:

// eventFunc is called,1,2
    this.context.eventHub.emit('myEvent', 1, 2);
  }

  eventFunc(argOne: number, argTwo: number) {
    console.log('eventFunc is called, ${argOne}, ${argTwo}');
  }
}
分享
微博
QQ
微信
回复
2024-07-30 17:06:40
相关问题
HarmonyOS 多module同时依赖同一个har
200浏览 • 1回复 待解决