ts-universal-events-show-hide.md 1.6 KB
Newer Older
Z
zengyawen 已提交
1
# Show/Hide Event
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
The show/hide event is triggered when a component is mounted or unmounted from the component tree. A component appears when mounted to the component tree and disappears when unmounted from the component tree.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6 7
> **NOTE**
>
> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9


Z
zengyawen 已提交
10 11
## Events

E
ester.zhou 已提交
12 13 14 15
| Name                                            | Bubbling Supported| Description                  |
| ------------------------------------------------ | -------- | -------------------------- |
| onAppear(event: () => void)    | No      | Triggered when the component is displayed.|
| onDisappear(event: () => void) | No      | Triggered when the component disappears.|
Z
zengyawen 已提交
16 17 18


## Example
Z
zengyawen 已提交
19

E
ester.zhou 已提交
20 21
```ts
// xxx.ets
Z
zengyawen 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
import prompt from '@system.prompt'

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

  build() {
    Column() {
      Button(this.changeAppear)
        .onClick(() => {
          this.isShow = !this.isShow
        }).margin(3).backgroundColor(0x2788D9)
      if (this.isShow) {
        Text(this.myText)
          .onAppear(() => {
            this.changeAppear = 'Show Text'
            prompt.showToast({ message: 'The text is shown', duration: 2000 })
          })
          .onDisAppear(() => {
            this.changeAppear = 'Hide Text'
            prompt.showToast({ message: 'The text is hidden', duration: 2000 })
          })
      }
    }.padding(30).width('100%')
  }
}
```

Z
zengyawen 已提交
53
![en-us_image_0000001211898468](figures/en-us_image_0000001211898468.gif)