openharmony 生命周期, 各种周期。
网上的截图:




@Entry
@Component
struct LifeCircle {
@State message: string = 'Hello World'
@State isAppear:boolean = true
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Divider()
Button('显示/隐藏子组件')
.backgroundColor(Color.Orange)
.onClick(()=>{
this.isAppear = !this.isAppear
})
if(this.isAppear){
LifeCircle_son()
}
}
.width('100%')
}
.height('100%')
}
//页面显示
onPageShow(){
//打印出页面显示的提醒
console.log('This Page is onPageShow')
}
//页面隐藏
onPageHide(){
//打印出页面隐藏的提醒
console.log('This Page is onPageHide')
}
//页面返回:手机按钮上的返回
onBackPress(){
//打印出页面返回的提醒
console.log('This Page is onBackPress')
}
//aboutToAppear和aboutToDisappear也可以放在入口组件中,后者表示程序被sha死时调用的函数
//组件即将出现时回调该接⼝
aboutToAppear(){
console.log('This Page is 入口aboutToAppear')
}
//组件析构销毁时触发的函数
aboutToDisappear(){
console.log('This Page is 入口aboutToDisappear')
}
}
//子组件的内容
@Component
struct LifeCircle_son{
build(){
Column(){
Text('显示子组件的内容').fontSize(40).fontStyle(FontStyle.Italic)
}
}
//组件即将出现时回调该接⼝
aboutToAppear(){
console.log('This Page is 子组件aboutToAppear')
}
//组件析构销毁时触发的函数
aboutToDisappear(){
console.log('This Page is 子组件aboutToDisappear')
}
}
这段代码可以演示一下, 各个函数的先后顺序,我就不演示了。
浙公网安备 33010602011771号