如何封装一个通用的commonEvent工具类

如何封装一个通用的commonEvent工具类

HarmonyOS
2024-03-19 15:03:32
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
橘猫bbt7

通过commonEventManager模块实现,具体可参考如下代码:

import { commonEventManager , BusinessError } from '@kit.BasicServicesKit'; 
 
export class SubscribeEvent { 
  private static subscriber: commonEventManager.CommonEventSubscriber 
  // 自定义的回调函数变量 
  private static callback: (a: BusinessError, b: commonEventManager.CommonEventSubscriber) => void 
 
  /** 
   * 创建订阅者 
   * @param subscribeInfo 订阅事件 
   * @callback 用户自定义回调函数 
   */ 
  static createSubscriber(subscribeInfo: commonEventManager.CommonEventSubscribeInfo, callback: (err: BusinessError, commonEventSubscriber: commonEventManager.CommonEventSubscriber) => void) { 
    SubscribeEvent.callback = callback 
    commonEventManager.createSubscriber(subscribeInfo, (err: BusinessError, subscriber) => { 
      if (err) { 
        console.error('CreateSubscriberCallBack err = ' + JSON.stringify(err)) 
      } else { 
        SubscribeEvent.subscriber = subscriber; 
        SubscribeEvent.subscribe(subscriber) 
        console.info('Create subscriber succeed') 
      } 
    }) 
  } 
 
  /** 
   * 订阅公共事件 
   * @param subscriber 订阅者 
   */ 
  private static subscribe(subscriber: commonEventManager.CommonEventSubscriber) { 
    if (subscriber != null) { 
      commonEventManager.subscribe(subscriber, (err: BusinessError, data) => { 
        if (err) { 
          console.error('subscribe err = ' + JSON.stringify(err)) 
        } else { 
          console.info('SubscribeCallBack data= ' + JSON.stringify(data)) 
          // this.callback('hello callback', data) 
        } 
      }) 
    } else { 
      console.info("Need create subscriber") 
    } 
  } 
} 
 
@Entry 
@Component 
struct Faq10_1 { 
  @State message: string = '' 
 
  // 发布公共事件回调 
  publishCB(err: BusinessError) { 
     if (err) { 
      console.error(`publish failed, code is ${err.code}, message is ${err.message}`); 
    } else { 
      console.info("publish") 
      ; 
    } 
  } 
 
  build() { 
    Row() { 
      Column() { 
        Text('订阅:' + this.message) 
          .fontSize(30) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { 
              events: ["myEvent"] 
            }; 
            let callback = (a: BusinessError, b: commonEventManager.CommonEventSubscriber) => { 
              this.message = a.name 
            } 
            SubscribeEvent.createSubscriber(subscribeInfo, callback) 
          }) 
        Text('发布') 
          .fontSize(30) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            // 公共事件相关信息 
            let options: commonEventManager.CommonEventPublishData = { 
              code: 0, // 公共事件的初始代码 
              data: "initial data", // 公共事件的初始数据 
              isOrdered: true  // 有序公共事件 
            } 
            // 发布公共事件 
            try { 
              commonEventManager.publish("myEvent", options, this.publishCB); 
            } catch (err) { 
              console.error(`publish failed, code is ${err.code}, message is ${err.message}`); 
            } 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.

参考链接

公共事件模块

分享
微博
QQ
微信
回复
2024-03-19 23:05:31
相关问题
HarmonyOS 需要一个axios封装工具
859浏览 • 1回复 待解决
preferences工具封装
2120浏览 • 1回复 待解决
HarmonyOS如何获取一个名称
2026浏览 • 4回复 待解决
HarmonyOS有没有通用工具样例Demo
1153浏览 • 1回复 待解决
HarmonyOS 封装一个公用getContext()
816浏览 • 1回复 待解决
HarmonyOS 是否有封装数据库工具
1048浏览 • 1回复 待解决
如何封装一个自定义Dialog对话框
3065浏览 • 1回复 待解决
HarmonyOS rcp通用请求Promise封装
1088浏览 • 1回复 待解决
HarmonyOS 如何一个工具获取User-Agent
1030浏览 • 1回复 待解决