代码拉取完成,页面将自动刷新
EventHub模块提供了事件中心,提供订阅、取消订阅、触发事件的能力。
说明:
本模块首批接口从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);
}
}
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");
}
}
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");
}
}
emit(event: string, ...args: Object[]): void;
触发指定事件。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
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);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。