鸿蒙开发接口公共事件与通知:【application/EventHub (EventHub)】

电子说

1.2w人已加入

描述

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

参数:

参数名类型必填说明
eventstring事件名称。
callbackFunction事件回调,事件触发后运行。

示例:

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

参数:

参数名类型必填说明
eventstring事件名称。
callbackFunction事件回调。如果不传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直接拿
eventstring事件名称。
...argsObject[]可变参数,事件触发时,传递给回调函数的参数。

示例:

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);
    }
}
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分