掛載卸載事件

2024-01-22 11:00 更新

掛載卸載事件指組件從組件樹上掛載、卸載時(shí)觸發(fā)的事件。

說明

從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。

事件

名稱

支持冒泡

功能描述

onAppear(event: () => void)

組件掛載顯示時(shí)觸發(fā)此回調(diào)。

從API version 9開始,該接口支持在ArkTS卡片中使用。

onDisAppear(event: () => void)

組件卸載消失時(shí)觸發(fā)此回調(diào)。

從API version 9開始,該接口支持在ArkTS卡片中使用。

示例

// xxx.ets
import promptAction from '@ohos.promptAction'

@Entry
@Component
struct AppearExample {
  @State isShow: boolean = true
  @State changeAppear: string = '點(diǎn)我卸載掛載組件'
  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(() => {
            promptAction.showToast({
              message: 'The text is shown',
              duration: 2000
            })
          })
          .onDisAppear(() => {
            promptAction.showToast({
              message: 'The text is hidden',
              duration: 2000
            })
          })
      }
    }.padding(30).width('100%')
  }
}

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)