当前位置:网站首页>ArkUI开发框架组件的生命周期详解
ArkUI开发框架组件的生命周期详解
2022-07-18 00:33:00 【InfoQ】
- 系统组件生命周期
- 自定义组件的生命周期
onAppearonDisAppear- onAppear:组件从组件树上挂载的回调。
- onDisAppear:组件从组件树上卸载的回到。
@Entry @Component struct Index {
@State textShow: boolean = false; // 默认状态
build() {
Column() {
Column() {
if (this.textShow) {
Text('挂载/卸载')
.fontSize(22)
.onAppear(() => {
console.log("哈哈,我被挂载了")
})
.onDisAppear(() => {
console.log("呜呜,我被卸载了")
})
}
}
.width('100%')
.height(60)
Button(this.textShow ? "卸载" : "挂载")
.stateStyles({
pressed: {
.backgroundColor(Color.Pink) // 设置点击时的样式
}
})
.onClick(() => { // 依次挂载卸载Text组件
this.textShow = !this.textShow;
})
}
.width('100%')
.height('100%')
}
}
- 组件的生命周期
- 使用
@Component修饰的组件,ArkUI开发框架会自动为其赋予私有的生命周期方法aboutToAppear()和aboutToDisappear(),它们用于通知开发者该自定义组件的生命周的变更。
- aboutToAppear:函数在创建自定义组件的新实例后,在执行其
build()函数之前执行。允许在该函数中改变状态变量,更改将在后续执行build()函数中生效。
- aboutToDisappear:函数在自定义组件析构消耗之前执行。不允许在该函数中改变状态变量,特别是
@Link变量的修改可能会导致应用程序行为不稳定。
- 页面的生命周期
- 页面本质上也是一个组件,只是页面对于组件来讲多了一个修饰符
@Entry,该修饰符表示当前组件是一个页面,它需要在config.json中做配置,页面除了具有组件的生命周期外,它还有自己独有的生命周期方法:
- onPageShow:页面显示时触发一次,包括路由过程、应用进入前后台等场景,仅
@Entry修饰的自定义组件生效。
- onPageHide:页面消失时触发一次,包括路由过程、应用进入前后台等场景,仅
@Entry修饰的自定义组件生效。
- onBackPress:当用户点击返回按钮时触发,仅@Entry修饰的自定义组件生效。该方法返回boolean类型的值,说明如下:
- 返回
true表示页面自己处理返回逻辑, 不进行页面路由。
- 返回
false表示使用默认的返回逻辑。
- 不返回值会作为
false处理。

边栏推荐
- C # FTP dual network card problem
- Offline installation of MariaDB
- 232. 用栈实现队列
- Is it safe to open an account with a new bond account? Is it reliable?
- The use of "!" in vscode is invalid, and there is no solution to the template problem
- OSPF学习笔记(五)---重发布
- GriddlyJS:基于强化学习的Web IDE
- Through JMeter pressure measurement surging
- Is it safe to buy funds in a securities account. Can you make a short line
- Arkui FAQ summary [Series 2]
猜你喜欢

Upload files to remote devices through pyro4 command parameters

C # minimize the WinForm software to the system tray, and start up automatically

I'm new here, so please take care of me. (actually, it's not new here ^ ^, hello CSDN, I'm here.)

Series operations of stack and queue containers (detailed explanation)

Reflector uses detailed explanation to convert DLL files into CS file

Identity Server 4使用OpenID Connect添加用户身份验证(三)

Work notes | talk about data quality audit

【FPGA】:ip核-----CIC滤波器

OSPF综合实验

windows安装mysql
随机推荐
通过群晖套件搭建内网邮件服务
PAT乙级-B1005 继续(3n+1)猜想(25)【数组】
重庆的哪个银行网点可以买到瑞兹基金产品?
启牛一键打新债靠谱吗,真的安全吗
emoji 为什么叫 emoji
[advanced C language] - common memory functions
Identity Server 4使用OpenID Connect添加用户身份验证(三)
[dry goods] how much do you know about MySQL infrastructure design?
Why is Emoji called Emoji
Stock financial information, board of directors, board of supervisors and other senior management information crawling
手机买股票开户哪家券商公司好?哪个更安全
postman调用接口返回404的几种原因
Pbootcms search SQL injection vulnerability
2022-7-17
2022 - 7 - 17
Wpa_supplicant WiFi连接
Arkui route jump
Is it reliable to open a new bond with one click? Is it really safe
Is it safe to open an account with a new bond account? Is it reliable?
CoCon: A Self-Supervised Approach for Controlled Text Generation | ICLR 2021