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

Z
zengyawen 已提交
3

Z
zengyawen 已提交
4 5 6 7 8
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
> This method is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.


## Required Permissions
Z
zengyawen 已提交
9 10 11 12

None


Z
zengyawen 已提交
13 14 15 16 17 18 19 20 21
## Events

  | Name | Bubble Supported | Description | 
| -------- | -------- | -------- |
| onAppear(callback: () => void) | No | Called when the component is displayed. | 
| onDisappear(callback: () => void) | No | Called when the component disappears. | 


## Example
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 53 54 55


```
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 已提交
56
![en-us_image_0000001211898468](figures/en-us_image_0000001211898468.gif)