文档管理中心
您当前浏览的HarmonyOS 5.0.0(API 12)文档归档不再维护,推荐您使用最新版本。详细请参考文档维护策略变更
FAQ应用服务开发用户通知服务(Notification Kit)如何让事件只在一个UIAbility实例中传递

如何让事件只在一个UIAbility实例中传递

在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}'); 
  } 
}

参考链接

使用EventHub进行数据同步

在 FAQ 中进行搜索
请输入您想要搜索的关键词