HarmonyOS emitter.on回调里接收的数据为空

emitter.emit时传递map数据,在emitter.on回调里接收的map是空的。

import { emitter } from '@kit.BasicServicesKit';

@Entry
@Component
struct EmitterTest {
  aboutToAppear(): void {
    emitter.on("EmitterTest", (eventData: emitter.EventData) => {
      console.info('EmitterTest callback, eventData:' + eventData["1"]);
      console.info('EmitterTest callback, eventData:' + eventData["2"]);
    });
  }

  build() {
    RelativeContainer() {
      Button('sned emitter', { stateEffect: true, type: ButtonType.Capsule })
        .width('80%')
        .height(40)
        .margin(10)
        .onClick(() => {
          let eventDataMap = new Map<string, Object>()
          eventDataMap.set("1", "111")
          eventDataMap.set("2", "222")
          let eventData: emitter.EventData = {
            data: { eventDataMap }
          };
          let options: emitter.Options = {
            priority: emitter.EventPriority.HIGH
          };

          emitter.emit("EmitterTest", options, eventData);
        })
    }
    .height('100%')
    .width('100%')
  }
}
HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

参考示例:

import { emitter } from '@kit.BasicServicesKit';

@Entry
@Component
struct EmitterTest {
  aboutToAppear(): void {
    emitter.on("EmitterTest", (eventData: emitter.EventData) => {
      let eventDataMap = eventData.data?.eventDataMap as Map<string, Object>
      console.info('EmitterTest callback, eventData:' + eventDataMap.get('1'));
      console.info('EmitterTest callback, eventData:' + eventDataMap.get('2'));
    });
  }

  build() {
    RelativeContainer() {
      Button('sned emitter', { stateEffect: true, type: ButtonType.Capsule })
        .width('80%')
        .height(40)
        .margin(10)
        .onClick(() => {
          let eventDataMap = new Map<string, Object>()
          eventDataMap.set("1", "111")
          eventDataMap.set("2", "222")
          let eventData: emitter.EventData = {
            data: { eventDataMap }
          };
          let options: emitter.Options = {
            priority: emitter.EventPriority.LOW
          };

          emitter.emit("EmitterTest", options, eventData);
        })
    }
    .height('100%')
    .width('100%')
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS emitter.on相关咨询
51浏览 • 1回复 待解决
Emitter如何声明函数类型
1808浏览 • 1回复 待解决
HarmonyOS emitter传输数据问题
105浏览 • 1回复 待解决
HarmonyOS 摄像头录制时,帧数据
22浏览 • 1回复 待解决
HarmonyOS 事件
65浏览 • 1回复 待解决
mongodb查找附近单车
6155浏览 • 1回复 待解决