我们在HarmonyOS开发中,一个简单的应用间通信(Event Bus)功能?

阅读 612
1 个回答

需要在不同应用间进行事件通信。

使用事件总线API来实现。

参见:https://developer.huawei.com/consumer/cn/doc/

import eventbus from '@ohos.eventbus';

@Entry
@Component
struct InterAppEventBus {
  postEvent() {
    eventbus.post('com.example.event', { key: 'value' }, (err) => {
      if (!err) {
        console.log('事件发布成功');
      }
    });
  }

  build() {
    Column() {
      Button('发布事件到事件总线').onClick(() => this.postEvent());
    }
  }
}

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进