可以参考以下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%') } }
可以参考以下demo: