基于harmonyOS解釋自定義組件生命周期
參考原文鏈接
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-custom-component-lifecycle-V5#abouttoappear
?
自定義組件的生命周期回調函數用于通知用戶該自定義組件的生命周期,這些回調函數是私有的,在運行時由開發框架在特定的時間進行調用,不能從應用程序中手動調用這些回調函數。
aboutToAppear
格式:aboutToAppear?(): void
aboutToAppear函數在創建自定義組件的新實例后,在執行其build()函數之前執行。允許在aboutToAppear函數中改變狀態變量,更改將在后續執行build()函數中生效。實現自定義布局的自定義組件的aboutToAppear生命周期在布局過程中觸發。


點擊查看代碼
@Entry
@Component
struct Index {
@State message: string = '未觸發aboutToAppear函數';
aboutToAppear(): void {
this.message='已觸發aboutToAppear函數'
}
click_function(){
this.message='點擊觸發click_function函數'
}
build() {
Row() {
Text(this.message)
.fontWeight(FontWeight.Bold)
.fontSize(30)
.width('80%')
.onClick(()=>{
this.click_function()
})
}
.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%')
}
}
onPageShow
格式:onPageShow?(): void
頁面每次顯示時觸發一次,包括路由過程、應用進入前臺等場景,僅@Entry裝飾的自定義組件生效。


點擊查看代碼
@Entry
@Component
struct Index {
@State message: string = '未觸發aboutToAppear函數';
onPageShow(): void {
this.message='已觸發onPageShow函數'
}
click_function(){
this.message='點擊觸發click_function函數'
}
build() {
Row() {
Text(this.message)
.fontWeight(FontWeight.Bold)
.fontSize(30)
.width('80%')
.onClick(()=>{
this.click_function()
})
}
.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%')
}
}
?
浙公網安備 33010602011771號