HarmonyOS/OpenHarmony应用开发-ArkTS挂载卸载事件

鸿蒙时代
发布于 2023-3-14 10:52
浏览
0收藏

一、示例效果图片
HarmonyOS/OpenHarmony应用开发-ArkTS挂载卸载事件-鸿蒙开发者社区HarmonyOS/OpenHarmony应用开发-ArkTS挂载卸载事件-鸿蒙开发者社区
二、事件
挂载卸载事件指组件从组件树上挂载、卸载时触发的事件。(api7开始支持)

名称 支持冒泡 功能描述
onAppear(event: () => void) 组件挂载显示时触发此回调。
onDisappear(event: () => void) 组件卸载消失时触发此回调。

三、示例代码
HarmonyOS/OpenHarmony应用开发-ArkTS挂载卸载事件-鸿蒙开发者社区
appear.ets

import prompt from '@ohos.prompt';

@Component
export default struct AppearExample {
  @State isShow: boolean = true;
  @State changeAppear: string = 'Hide Text';
  private myText: string = 'Text for onAppear';

  build() {
    Column() {
      Button(this.changeAppear)
        .onClick(() => {
          this.isShow = !this.isShow;
        }).margin(15)
      if (this.isShow) {
        Text(this.myText).fontSize(26).fontWeight(FontWeight.Bold)
          .onAppear(() => {
            this.changeAppear = 'Hide Text';
            prompt.showToast({
              message: 'The text is shown',
              duration: 2000
            })
          })
          .onDisAppear(() => {
            this.changeAppear = 'Show Text';
            prompt.showToast({
              message: 'The text is hidden',
              duration: 2000
            })
          })
      }
    }.padding(30).width('100%')
  }
}

index.ets

import appear from '../eventMuster/appear'

@Entry
@Component
struct Index {

  build() {
    Column() {
      appear()
    }
    .width('100%')
    .height('100%')
  }
}

四、完整代码下载地址
https://gitee.com/jltfcloudcn/jump_to/tree/master/appear

标签
HarmonyOSOpenHarmony应用开发-ArkTS挂载卸.docx 61K 7次下载
收藏
回复
举报
回复
    相关推荐