当前位置: 首页 > news >正文

Page与自定义Components生命周期

自定义组件

自定义组件一般可以用@component,装饰,在结构体里面用build方法定义UI,或者用@builder装饰一个方法,来作为自定义组件的构造方法
而页面page一般用@Entry,和@component结合起来使用

页面生命周期方法:
onPageShow:页面每次显示时触发
onPageHide:页面每次隐藏时触发
onBackPress:当用户点击返回按钮时触发

组件生命周期方法:
aboutToAppear:组件即将出现时回调该接口,在执行Build()函数之前执行

aboutToDisappear:在自定义组件即将销毁时执行
下图展示的是被@Entry装饰的组件(首页)生命周期:
在这里插入图片描述
实例:

// Index.ets
import router from '@ohos.router';@Entry
@Component
struct MyComponent {@State showChild: boolean = true;// 只有被@Entry装饰的组件才可以调用页面的生命周期onPageShow() {console.info('Index onPageShow');}// 只有被@Entry装饰的组件才可以调用页面的生命周期onPageHide() {console.info('Index onPageHide');}// 只有被@Entry装饰的组件才可以调用页面的生命周期onBackPress() {console.info('Index onBackPress');}// 组件生命周期aboutToAppear() {console.info('MyComponent aboutToAppear');}// 组件生命周期aboutToDisappear() {console.info('MyComponent aboutToDisappear');}build() {Column() {// this.showChild为true,创建Child子组件,执行Child aboutToAppearif (this.showChild) {Child()}// this.showChild为false,删除Child子组件,执行Child aboutToDisappearButton('create or delete Child').onClick(() => {this.showChild = false;})// push到Page2页面,执行onPageHideButton('push to next page').onClick(() => {router.pushUrl({ url: 'pages/Page2' });})}}
}@Component
struct Child {@State title: string = 'Hello World';// 组件生命周期aboutToDisappear() {console.info('[lifeCycle] Child aboutToDisappear')}// 组件生命周期aboutToAppear() {console.info('[lifeCycle] Child aboutToAppear')}build() {Text(this.title).fontSize(50).onClick(() => {this.title = 'Hello ArkUI';})}
}
@Builder function ABuilder($$: { paramA1: string }) {Row() {Text(`UseStateVarByReference: ${$$.paramA1} `)}
}
@Entry
@Component
struct Parent {@State label: string = 'Hello';build() {Column() {// 在Parent组件中调用ABuilder的时候,将this.label引用传递给ABuilderABuilder({ paramA1: this.label })Button('Click me').onClick(() => {// 点击“Click me”后,UI从“Hello”刷新为“ArkUI”this.label = 'ArkUI';})}}
}
http://www.lryc.cn/news/426543.html

相关文章:

  • Chain of Thought (CoT) 系列论文:大模型思维链,提升 LLM 的推理能力
  • 已解决:java.net.BindException: 地址已在使用
  • 看书标记【数据科学:R语言实战 8】
  • STM32标准库学习笔记-1.基础知识
  • Nginx:高效HTTP服务器与反向代理
  • vue3二次封装element-puls
  • 在CentOS 7上安装Apache Tomcat 8的方法
  • 深入理解分布式事务中的三阶段提交(3PC),什么是3PC,3PC原理是怎样?3PC的优化?
  • 这款新的 AI 工具会消灭 ChatGPT 吗?
  • 谷粒商城实战笔记-214~219-商城业务-认证服务-验证码防刷校验
  • 在华为服务器的openEuler系统中适配Pytorch调用NPU
  • MVCC工作原理深入解析
  • 使用html+css+js实现完整的登录注册页面
  • 2024年8月16日(运维自动化 ansible)
  • 荣耀Magicbook x14 扩容1TB固态
  • Springboot整合全文检索引擎Lucene
  • 【深度学习】【语音】TTS, 如何使用Python分析WAV的采样率、比特深度、通道数
  • Linux的安装和使用
  • 查看一个exe\dll文件的依赖项
  • 高校科研信息管理系统pf
  • Linux 开机自动挂载共享文件设置
  • c_cpp_properties.json、launch.json、 tasks.json
  • mysql 一些知识点 面试用
  • STM32之点亮LED灯
  • Java 多线程练习2 (抽奖比较Runnable写法)
  • 使用fastboot更新部分系统
  • windows 加载portch遇到的错误
  • 如何将 CICD 模版重构为 CICD component?
  • 数学建模——评价决策类算法(层次分析法、Topsis)
  • KEEPALIVED 全csdn最详细----理论+实验(干货扎实,包教会的)