• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 挂载卸载事件
2
3>  **说明:**
4>
5>  从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
6
7
8## 事件
9
10| 名称                                             | 支持冒泡 | 功能描述                   |
11| ------------------------------------------------ | -------- | -------------------------- |
12| onAppear(event: () => void)    | 否       | 组件挂载显示时触发此回调。 |
13| onDisappear(event: () => void) | 否       | 组件卸载消失时触发此回调。 |
14
15
16## 示例
17
18```ts
19// xxx.ets
20import prompt from '@ohos.prompt';
21
22@Entry
23@Component
24struct AppearExample {
25  @State isShow: boolean = true;
26  @State changeAppear: string = 'Hide Text';
27  private myText: string = 'Text for onAppear';
28
29  build() {
30    Column() {
31      Button(this.changeAppear)
32        .onClick(() => {
33          this.isShow = !this.isShow;
34        }).margin(15)
35      if (this.isShow) {
36        Text(this.myText).fontSize(26).fontWeight(FontWeight.Bold)
37          .onAppear(() => {
38            this.changeAppear = 'Hide Text';
39            prompt.showToast({
40              message: 'The text is shown',
41              duration: 2000
42            })
43          })
44          .onDisAppear(() => {
45            this.changeAppear = 'Show Text';
46            prompt.showToast({
47              message: 'The text is hidden',
48              duration: 2000
49            })
50          })
51      }
52    }.padding(30).width('100%')
53  }
54}
55```
56
57![zh-cn_image_0000001219864151](figures/zh-cn_image_0000001219864151.gif)
58