HarmonyOS-鸿蒙app开发—ArkTs基础挂载卸载事件
onAppear(event: () => void) 组件挂载显示时触发此回调。
onDisappear(event: () => void) 组件卸载消失时触发此回调。
index.ets
import prompt from '@ohos.prompt'; @Entry @Component struct Index { @State message: string = '炫码科技' @State changeAppear: string = 'Hide Text'; build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onAppear(() => { //挂载事件 this.changeAppear = 'Hide Text'; prompt.showToast({ message: 'The text is shown', duration: 2000 }) }) .onDisAppear(() => { //卸载事件 this.changeAppear = 'Show Text'; prompt.showToast({ message: 'The text is hidden', duration: 2000 }) }) Button("点我") .margin(20) .backgroundColor('red') .onClick(()=> //按钮点击事件 { prompt.showToast({ message: '鸿蒙app开发', duration: 5000 }) }) } .width('100%') } .height('100%') .backgroundColor('green') } }