EventHub
EventHub模块提供了事件中心,提供订阅、取消订阅、触发事件的能力。
说明:
开发前请熟悉鸿蒙开发指导文档 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]点击或者复制转到。
本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
本模块接口仅可在Stage模型下使用。
使用说明
在使用eventHub的功能前,需要通过Ability实例的成员变量context获取。
import Ability from '@ohos.application.Ability'
export default class MainAbility extends Ability {
func1(){
console.log("func1 is called");
}
onForeground() {
this.context.eventHub.on("123", this.func1);
}
}
EventHub.on
on(event: string, callback: Function): void;
订阅指定事件。
系统能力 :SystemCapability.Ability.AbilityRuntime.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
event | string | 是 | 事件名称。 |
callback | Function | 是 | 事件回调,事件触发后运行。 |
示例:
import Ability from '@ohos.application.Ability'
export default class MainAbility extends Ability {
onForeground() {
this.context.eventHub.on("123", this.func1);
this.context.eventHub.on("123", () = > {
console.log("call anonymous func 1");
});
// 结果:
// func1 is called
// call anonymous func 1
this.context.eventHub.emit("123");
}
func1() {
console.log("func1 is called");
}
}
EventHub.off
off(event: string, callback?: Function): void;
取消订阅指定事件。当callback传值时,取消订阅指定的callback;未传值时,取消订阅该事件下所有callback。
系统能力 :SystemCapability.Ability.AbilityRuntime.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
event | string | 是 | 事件名称。 |
callback | Function | 否 | 事件回调。如果不传callback,则取消订阅该事件下所有callback。 |
示例:
import Ability from '@ohos.application.Ability'
export default class MainAbility extends Ability {
onForeground() {
this.context.eventHub.on("123", this.func1);
this.context.eventHub.off("123", this.func1); //取消订阅func1
this.context.eventHub.on("123", this.func1);
this.context.eventHub.on("123", this.func2);
this.context.eventHub.off("123"); //取消订阅func1和func2
}
func1() {
console.log("func1 is called");
}
func2() {
console.log("func2 is called");
}
}
EventHub.emit
emit(event: string, ...args: Object[]): void;
触发指定事件。
系统能力 :SystemCapability.Ability.AbilityRuntime.Core
参数:
参数名 | 类型 | 必填 | 说明HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿 |
---|---|---|---|
event | string | 是 | 事件名称。 |
...args | Object[] | 是 | 可变参数,事件触发时,传递给回调函数的参数。 |
示例:
import Ability from '@ohos.application.Ability'
export default class MainAbility extends Ability {
onForeground() {
this.context.eventHub.on("123", this.func1);
// 结果:
// func1 is called,undefined,undefined
this.context.eventHub.emit("123");
// 结果:
// func1 is called,1,undefined
this.context.eventHub.emit("123", 1);
// 结果:
// func1 is called,1,2
this.context.eventHub.emit("123", 1, 2);
}
func1(a, b) {
console.log("func1 is called," + a + "," + b);
}
}
-
移动开发
+关注
关注
0文章
52浏览量
9858 -
鸿蒙系统
+关注
关注
183文章
2638浏览量
66797 -
HarmonyOS
+关注
关注
79文章
1984浏览量
30690 -
OpenHarmony
+关注
关注
25文章
3753浏览量
16713 -
鸿蒙OS
+关注
关注
0文章
190浏览量
4578
发布评论请先 登录
相关推荐
鸿蒙开发接口公共事件与通知:【@ohos.reminderAgent (后台代理提醒)】
![<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b><b class='flag-5'>接口</b><b class='flag-5'>公共事</b>件与<b class='flag-5'>通知</b>:【@ohos.reminderAgent (后台代理提醒)】](https://file1.elecfans.com/web2/M00/C5/CD/wKgZomYCdwyAIFf5AAB_7E1pFms943.jpg)
鸿蒙开发接口公共事件与通知:【FFI能力】 N-API在Android、iOS平台应用的使用指导
![<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b><b class='flag-5'>接口</b><b class='flag-5'>公共事</b>件与<b class='flag-5'>通知</b>:【FFI能力】 N-API在Android、iOS平台应用的使用指导](https://file1.elecfans.com/web2/M00/E8/10/wKgZomZN7JWAetyRAAD3rZ00HWs627.jpg)
鸿蒙原生应用/元服务开发-通知添加行为意图
鸿蒙实战基础(ArkTS)-窗口管理
基于ArkTS语言的OpenHarmony APP应用开发:公共事件的订阅和发布
HarmonyOS应用开发-公共事件处理
鸿蒙原生应用/元服务开发-Stage模型能力接口(五)
鸿蒙开发之EventHub
![<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>之<b class='flag-5'>EventHub</b>](https://file1.elecfans.com/web2/M00/C0/6E/wKgZomXVmMKAQfJdAABOX-HIl_0501.png)
鸿蒙开发接口Ability框架:【@ohos.ability.wantConstant (wantConstant)】
![<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b><b class='flag-5'>接口</b>Ability框架:【@ohos.ability.wantConstant (wantConstant)】](https://file1.elecfans.com/web2/M00/C5/D1/wKgZomYChGOAUaiiAADe1d8SeRY102.jpg)
鸿蒙开发接口公共事件与通知:【FFI能力(Node-API)】
![<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b><b class='flag-5'>接口</b><b class='flag-5'>公共事</b>件与<b class='flag-5'>通知</b>:【FFI能力(Node-API)】](https://file1.elecfans.com/web2/M00/E8/90/wKgaomZMXoqANzvuAAA6UGbDgbQ272.png)
基于ArkTS语言的OpenHarmony APP应用开发:公共事件的订阅和发布
![基于ArkTS语言的OpenHarmony APP应用<b class='flag-5'>开发</b>:<b class='flag-5'>公共事</b>件的订阅和发布](https://file.elecfans.com/web2/M00/26/21/pYYBAGG5jjSALfrEAAAwAa9Oig8799.png)
评论