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

 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

参数:

搜狗高速浏览器截图20240326151450.png

参数名类型必填说明 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);
    }
}

鸿蒙语言有TS、ArkTS等语法,那么除了这些基础知识之外,其核心技术点有那些呢?下面就用一张整理出的鸿蒙学习路线图表示:

从上面的OpenHarmony技术梳理来看,鸿蒙的学习内容也是很多的。现在全网的鸿蒙学习文档也是非常的少,下面推荐一些:完整内容可在头像页保存,或这qr23.cn/AKFP8k甲助力

内容包含:《鸿蒙NEXT星河版开发学习文档》

  • ArkTS
  • 声明式ArkUI
  • 多媒体
  • 通信问题
  • 系统移植
  • 系统裁剪
  • FW层的原理
  • 各种开发调试工具
  • 智能设备开发
  • 分布式开发等等。

这些就是对往后开发者的分享,希望大家多多点赞关注喔!

  • 15
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值