• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 挂载卸载事件
2
3挂载卸载事件指组件从组件树上挂载、卸载时触发的事件。
4
5> **说明:**
6>
7> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9## onAppear
10
11onAppear(event: () => void): T
12
13组件挂载显示时触发此回调。
14
15**返回值:**
16
17| 类型 | 说明 |
18| -------- | -------- |
19| T | 返回当前组件。 |
20
21**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
22
23**系统能力:** SystemCapability.ArkUI.ArkUI.Full
24
25## onDisAppear
26
27onDisAppear(event: () => void): T
28
29**返回值:**
30
31| 类型 | 说明 |
32| -------- | -------- |
33| T | 返回当前组件。 |
34
35组件卸载消失时触发此回调。
36
37**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
38
39**系统能力:** SystemCapability.ArkUI.ArkUI.Full
40
41
42## 示例
43
44```ts
45// xxx.ets
46import promptAction from '@ohos.promptAction'
47
48@Entry
49@Component
50struct AppearExample {
51  @State isShow: boolean = true
52  @State changeAppear: string = '点我卸载挂载组件'
53  private myText: string = 'Text for onAppear'
54
55  build() {
56    Column() {
57      Button(this.changeAppear)
58        .onClick(() => {
59          this.isShow = !this.isShow
60        }).margin(15)
61      if (this.isShow) {
62        Text(this.myText).fontSize(26).fontWeight(FontWeight.Bold)
63          .onAppear(() => {
64            promptAction.showToast({
65              message: 'The text is shown',
66              duration: 2000
67            })
68          })
69          .onDisAppear(() => {
70            promptAction.showToast({
71              message: 'The text is hidden',
72              duration: 2000
73            })
74          })
75      }
76    }.padding(30).width('100%')
77  }
78}
79```
80
81![zh-cn_image_0000001219864151](figures/zh-cn_image_0000001219864151.gif)
82