HarmonyOS LiveEventBus 无法获取this

HarmonyOS
2024-12-18 14:32:13
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

可以参考以下demo:

import { LiveEventBus } from '@ohos/liveeventbus';
import { MState } from '@ohos/liveeventbus';
import { Lifecycle } from '@ohos/liveeventbus';

const KEY_TEST_CLOSE_ALL_PAGE = "key_test_close_all_page";

@Entry({ routeName: "EditorPage" })
@Component
export struct EditorPage {
  @State message: string = 'Hello World';
  private mLifecycle?: Lifecycle;

  getLifecycle(): Lifecycle {
    if (this.mLifecycle) {
      return this.mLifecycle
    }
    return new Lifecycle(MState.STARTED)
  }

  aboutToAppear() {
    //创建生命周期感知对象
    this.mLifecycle = new Lifecycle(MState.STARTED)
    //订阅消息
    LiveEventBus
      .get<boolean>(KEY_TEST_CLOSE_ALL_PAGE)
      .observe(this, {
        onChanged: (b: boolean) => {
          this.message
        }
      });
  }

  build() {
    Column() {
    }
    .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.
分享
微博
QQ
微信
回复
2024-12-18 17:26:16
相关问题
HarmonyOS 无法获取udid
696浏览 • 1回复 待解决
HarmonyOS 无法获取push token
713浏览 • 1回复 待解决
HarmonyOS 无法获取NativeXComponent的id
824浏览 • 1回复 待解决
HarmonyOS 无法获取推送Push Token
1059浏览 • 1回复 待解决
HarmonyOS AVDexmuer无法获取视频时长
644浏览 • 1回复 待解决
HarmonyOS 无法获取固定设备id
766浏览 • 1回复 待解决
HarmonyOS TextInput组件无法自动获取焦点
1017浏览 • 1回复 待解决
HarmonyOS router.getParams无法获取,报错
1023浏览 • 1回复 待解决
HarmonyOS 无法获取服务提供商
866浏览 • 1回复 待解决
EnvironmentCallback无法获取信息
2587浏览 • 1回复 待解决
HarmonyOS 无法获取蓝牙HCI日志并导出
916浏览 • 1回复 待解决
HarmonyOS无法获取蓝牙设备的真实MAC
1866浏览 • 1回复 待解决