HarmonyOS NEXT LiveEventBus 无法获取this?

阅读 655
1 个回答

你可以参考以下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%') 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题