网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
const DOMAIN_NUMBER: number = 0xFF00;
const TAG: string = ‘[EventAbility]’;
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
// 获取UIAbility实例的上下文
let context = this.context;
// 获取eventHub
let eventhub = this.context.eventHub;
// 执行订阅操作
eventhub.on(‘event1’, this.eventFunc);
eventhub.on(‘event1’, (data: string) => {
// 触发事件,完成相应的业务操作
});
hilog.info(DOMAIN_NUMBER, TAG, ‘%{public}s’, ‘Ability onCreate’);
}
// ...
eventFunc(argOne: Context, argTwo: Context): void {
hilog.info(DOMAIN_NUMBER, TAG, '1. ’ + ${argOne}, ${argTwo}
);
return;
}
}
2. 在UI中通过 eventHub.emit() 方法触发该事件,在触发事件的同时,根据需要传入参数信息。
import common from ‘@ohos.app.ability.common’;
import promptAction from ‘@ohos.promptAction’
@Entry
@Component
struct Page_EventHub {
private context = getContext(this) as common.UIAbilityContext;
eventHubFunc() : void {
// 不带参数触发自定义“event1”事件
this.context.eventHub.emit(‘event1’);
// 带1个参数触发自定义“event1”事件
this.context.eventHub.emit(‘event1’, 1);
// 带2个参数触发自定义“event1”事件
this.context.eventHub.emit(‘event1’, 2, ‘test’);
// 开发者可以根据实际的业务场景设计事件传递的参数
}
build() {
Column() {
Row() {
Flex({ justifyContent: FlexAlign.Start, alignContent: FlexAlign.Center }) {
Text(KaTeX parse error: Expected 'EOF', got '}' at position 209: …: 24}) }̲ } …r(‘app.color.backGrounding’))
List({ initialIndex: 0 }) {
ListItem() {
Row() {
Row(){
Text($r('app.string.EventHubFuncA'))
.textAlign(TextAlign.Start)
.fontWeight(500)
.margin({ top: 13, bottom: 13, left: 0, right: 8 })
.fontSize(16)
.width(232)
.height(22)
.fontColor($r('app.color.text_color'))
}
.height(48)
.width('100%')
.borderRadius(24)
.margin({ top: 4, bottom: 4, left: 12, right: 12 })
}
.onClick(() => {
this.eventHubFunc();
promptAction.showToast({
message: $r('app.string.EventHubFuncA')
});
})
}
.height(56)
.backgroundColor($r('app.color.start_window_background'))
.borderRadius(24)
.margin({ top: 8, right: 12, left: 12 })
ListItem() {
Row() {
Row(){
Text($r('app.string.EventHubFuncB'))
.textAlign(TextAlign.Start)
.fontWeight(500)
.margin({ top: 13, bottom: 13, left: 0, right: 8 })
.fontSize(16)
.width(232)
.height(22)
.fontColor($r('app.color.text_color'))
}
.height(48)
.width('100%')
.borderRadius(24)
.margin({ top: 4, bottom: 4, left: 12, right: 12 })
}
.onClick(() => {
this.context.eventHub.off('event1');
promptAction.showToast({
message: $r('app.string.EventHubFuncB')
});
})
}
.height(56)
.backgroundColor($r('app.color.start_window_background'))
.borderRadius(24)
.margin({ top: 12, right: 12, left: 12 })
}
.height('100%')
.backgroundColor($r('app.color.backGrounding'))
}
.width('100%')
.margin({ top: 8 })
.backgroundColor($r('app.color.backGrounding'))
}
}
3. 在UIAbility的注册事件回调中可以得到对应的触发事件结果,运行日志结果如下所示。
[Example].[Entry].[EntryAbility] 1. []
[Example].[Entry].[EntryAbility] 1. [1]
[Example].[Entry].[EntryAbility] 1. [2,“test”]
4. 在自定义事件“event1”使用完成后,可以根据需要调用 eventHub.off() 方法取消该事件的订阅。
// context为UIAbility实例的AbilityContext
this.context.eventHub.off(‘event1’);
### 使用AppStorage/LocalStorage进行数据同步
ArkUI提供了AppStorage和LocalStorage两种应用级别的状态管理方案,可用于实现应用级别和UIAbility级别的数据同步。使用这些方案可以方便地管理应用状态,提高应用性能和用户体验。其中,AppStorage是一个全局的状态管理器,适用于多个UIAbility共享同一状态数据的情况;而LocalStorage则是一个局部的状态管理器,适用于单个UIAbility内部使用的状态数据。通过这两种方案,开发者可以更加灵活地控制应用状态,提高应用的可维护性和可扩展性。详细请参见 应用级变量的状态管理 。
**为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:[`https://qr21.cn/FV7h05`]( )**
### 《鸿蒙开发学习手册》:
![img](https://img-blog.csdnimg.cn/img_convert/4d62f489cdb5684b6f0235dbbdc502ce.png)
![img](https://img-blog.csdnimg.cn/img_convert/e11f6df3aef2506eb4a95953845b79c5.png)
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618636735)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
难做到真正的技术提升。**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618636735)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**