当前位置:网站首页>The lifecycle of arkui development framework components
The lifecycle of arkui development framework components
2022-07-19 15:25:00 【Hua Weiyun】
ArkUI The development framework gives components a unique lifecycle approach , Here are two main situations :
System component life cycle
Customize the lifecycle of the component
For system components , The life cycle approach is onAppear and onDisAppear ,
onAppear: Callback of the component mounted from the component tree .
onDisAppear: Components unloaded from the component tree return to .
A simple example is shown below :
@Entry @Component struct Index { @State textShow: boolean = false; // Default state build() { Column() { Column() { if (this.textShow) { Text(' mount / uninstall ') .fontSize(22) .onAppear(() => { console.log(" ha-ha , I'm attached ") }) .onDisAppear(() => { console.log(" Sobbing , I was uninstalled ") }) } } .width('100%') .height(60) Button(this.textShow ? " uninstall " : " mount ") .stateStyles({ pressed: { .backgroundColor(Color.Pink) // Set the style when clicking } }) .onClick(() => { // Mount and unload in turn Text Components this.textShow = !this.textShow; }) } .width('100%') .height('100%') }}Customize the lifecycle of the component .
Life cycle of components
Use
@ComponentDecorated components ,ArkUI The development framework will automatically give it a private lifecycle approachaboutToAppear()andaboutToDisappear(), They are used to inform developers of changes to the lifecycle of the custom component .aboutToAppear: Function after creating a new instance of the custom component , In the execution of its
build()Function before execution . It is allowed to change the state variable in this function , The changes will be implemented laterbuild()Function .aboutToDisappear: The function executes before the custom component destructs and consumes . Changing state variables in this function is not allowed , especially
@LinkModification of variables can lead to unstable application behavior .
The life cycle of a page
A page is essentially a component , It's just that the page has an additional modifier for components
@Entry, This modifier indicates that the current component is a page , It needs to be inconfig.jsonMake configuration in , In addition to the life cycle of components, pages , It also has its own unique lifecycle approach :onPageShow: Trigger once when the page is displayed , Including routing process 、 The application enters the front and back stage and other scenes , only
@EntryModified custom components take effect .onPageHide: Trigger once when the page disappears , Including routing process 、 The application enters the front and back stage and other scenes , only
@EntryModified custom components take effect .onBackPress: Triggered when the user clicks the back button , only @Entry Modified custom components take effect . This method returns boolean Type value , The explanation is as follows :
return
trueIt means that the page handles the return logic by itself , No page routing .return
falseIndicates that the default return logic is used .No return value will be used as
falseHandle .
The comparison of component life cycle production tables is as follows :
| Function name | describe |
|---|---|
| onAppear | Methods unique to system components , Callback of the component mounted from the component tree . |
| onDisAppear | Methods unique to system components , Components unloaded from the component tree return to . |
| aboutToAppear | Function after creating a new instance of the custom component , In the execution of its build() Function before execution . It is allowed to change the state variable in this function , The changes will be implemented later build() Function . |
| aboutToDisappear | The function executes before the custom component destructs and consumes . Changing state variables in this function is not allowed , especially @Link Modification of variables can lead to unstable application behavior . |
| onPageShow | This callback is triggered when the page is displayed , Including routing process 、 The application enters the front and back stage and other scenes . only @Entry Modified custom components take effect . |
| onPageHide | This callback is triggered when the page disappears , Including routing process 、 The application enters the front and back stage and other scenes . only @Entry Modified custom components take effect . |
| onBackPress | Triggered when the user clicks the back button , This method returns boolean type ,true: It means that the page handles the return logic by itself , No page routing .false: Indicates that the default return logic is used . No return value will be used as false Handle . only @Entry Modified custom components take effect . |
One thing to watch out for , It is allowed to use Promiseasync await .
边栏推荐
- [XSS range 10-14] insert when you see parameters: find hidden parameters and various attributes
- [user article] examples of P4 consolidation practice guide disassemble resolve
- [dynamic memory management]
- Wechat applet 9 release code
- Est - il sûr d'ouvrir un compte en ligne pour acheter des fonds? Je viens de toucher le Fonds, je ne sais pas comment demander des conseils.
- Is it safe to buy funds in a stock account? I want to buy funds for a long time
- Unix ls
- tensorflow clip对NaN、inf的效果
- GYM103660L.Monster Tower 整体二分
- 数字IC-1.11.1 静态时序分析 - 单周期静态时序分析
猜你喜欢

MMRotate从零开始训练自己的数据集

2022/7/17

A - trees on the level

Wechat applet 6 cloud development cloud database
![[flower carving hands-on] interesting music visualization project (11) --ws2812 magic ribbon](/img/96/4b585caea47af2970d65b15516f571.jpg)
[flower carving hands-on] interesting music visualization project (11) --ws2812 magic ribbon

浅谈ISP-噪声模型1

Notepad++实用功能分享(正则行尾行首替换常用方法、文本比对功能等)

Mongodb partition cluster construction

State machine exercise

Wechat applet 8 cloud function
随机推荐
解决jupyter控制台出现中文乱码的问题
天天基金上买基金是安全的吗?在线等答案
使用flex布局实现局部滚动条
一次函数 T1744 963字符写法
用对工具,CI事半功倍
I'm new here, so please take care of me. (actually, it's not new here ^ ^, hello CSDN, I'm here.)
关于阿里云经典网络的问题
数字IC-1.11.1 静态时序分析 - 单周期静态时序分析
Li Hongyi machine learning introduction -2022.07.11
新基民在支付宝上买基金安全吗
【花雕动手做】有趣好玩的音乐可视化项目(11)---WS2812幻彩灯带
【用户文章】P4合并实践指南之实例拆解Resolve
Which securities company should I choose to open a stock account? What securities company is safer
证券账户上买基金安全吗。可以做短线吗
Cloudbees CI uses velero for disaster recovery (DR) proof of concept
State machine exercise
Wechat applet 9 release code
CloudBees CI使用Velero进行灾备(DR)概念验证
堆栈的实现之顺序存储,链式存储
Is online account opening safe? Then choose which securities to open a securities account