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

HarmonyOS父子组件传递参数

HarmonyOS父子组件传递参数

1. 使用@State@Prop进行父子组件传递———注意是单向同步

@Prop装饰器:父子单向同步

  • 注意:只支持单向同步,同时也只能支持string\number\boolean\enum比较简单的类型。
    在这里插入图片描述

代码

// 使用 props 进行父子组件传值
@Component
struct SonCom {// 父子间传递过来的数据使用 @Prop 进行接受@Prop sonCar: string// 修改传递的参数changeInfo = (info: string)=> {}build() {Column() {Text(`这是子组件的盒子--- ${this.sonCar}`)Button('子组件修改父组件的数据').onClick((event: ClickEvent) => {this.changeInfo('吉利银河 L7 ----' + Math.ceil(Math.random() * 10))})}.width('100%').height(100).backgroundColor(Color.Orange)}
}@Entry
@Component
struct PropsPage {@State info: string = '比亚迪 宋'changeInfo = (newInfo: string)=>{this.info = newInfo}build() {Column({space: 20}) {Text(`这是父组件的盒子 ${this.info}`)Button('修改父组件的数据').onClick((event: ClickEvent) => {this.info = '领克 08---' + Math.ceil(Math.random() * 10)})// 这是子组件SonCom({sonCar: this.info,changeInfo: this.changeInfo})}.width('100%').height(300).backgroundColor(Color.Pink)}
}

演示
在这里插入图片描述

2. @Link装饰器:父子双向同步

@Link装饰器:父子双向同步

  • 注意
    在这里插入图片描述
// 子组件
@Component
struct ChildCom {@Link list: number[]build() {Column() {List({space: 10}) {ForEach(this.list, (item: number, index) => {ListItem() {Text(item.toString()).width('100%').padding(10).backgroundColor(Color.White)}})}}.onClick(() => {this.list.push(this.list.length + 1)})}
}// 父组件
@Entry
@Component
struct StateLink {@State list: number[] = [1, 2, 3]build() {Column() {ChildCom({// 注意,这里调用时,使用$替换this,这是语法规定list: $list})}.width('100%').height('100%').backgroundColor(Color.Gray)}
}

在这里插入图片描述

3. @Provide装饰器和@Consume装饰器:与后代组件双向同步

@Provide装饰器和@Consume装饰器:与后代组件双向同步

  • 注意@Consume装饰的变量通过相同的属性名绑定其祖先组件@Provide装饰的变量,在这里就是SunziCom中的@Consume listInfo: ListItemClass与祖先组件ProvideConsume中的@Provide listInfo: ListItemClass属性名保持一致。
// 这是模拟的数据
@Observed
class ListItemClass {name: stringlikeNum: numberisLike: booleancomment: stringconstructor(name: string, likeNum: number, isLike: number, comment: string) {this.name = namethis.likeNum = likeNumthis.isLike = isLike === 0 ? false : truethis.comment = comment}
}// 这是 孙子组件
@Component
struct SunziCom {// 注意:这里的属性名要保持和 @Provide修饰的父组件属性名一致.@Consume listInfo: ListItemClassbuild() {Column() {Text(this.listInfo.name).fontSize(18).fontWeight(700).fontColor('#333').margin({bottom: 10})Row() {Text(this.listInfo.comment)Row() {Text(this.listInfo.likeNum.toString()).fontColor(this.listInfo.isLike ? Color.Red : '#333')}.onClick(() => {if (this.listInfo.isLike) {this.listInfo.likeNum -= 1} else {this.listInfo.likeNum += 1}this.listInfo.isLike = !this.listInfo.isLike})}.justifyContent(FlexAlign.SpaceBetween).width('100%')}.padding(10).borderRadius(10).alignItems(HorizontalAlign.Start).width('100%').backgroundColor(Color.White)}
}
// 这是 儿子组件
@Component
struct ErziCom {build() {SunziCom()}
}@Entry
@Component
struct ProvideConsume {@Provide listInfo: ListItemClass = new ListItemClass(`小火车况且况且-${Math.ceil(Math.random() * 10)}`, Math.ceil(Math.random() * 100), Math.floor(Math.random() * 2), `这是随机的评论--${Math.ceil(Math.random() * 1000)}`)build() {Column(){ErziCom()}}
}

在这里插入图片描述

4. 使用@Observed装饰器和@ObjectLink装饰器:嵌套类对象属性变化,主要是可以处理Link遇上ForEach而导致一些奇怪的问题

@Observed装饰器和@ObjectLink装饰器:嵌套类对象属性变化

// 这是模拟的数据
@Observed
class ListItemClass {name: stringlikeNum: numberisLike: booleancomment: stringconstructor(name: string, likeNum: number, isLike: number, comment: string) {this.name = namethis.likeNum = likeNumthis.isLike = isLike === 0 ? false : truethis.comment = comment}
}function createData() {return [new ListItemClass(`小火车况且况且-${Math.ceil(Math.random() * 10)}`, Math.ceil(Math.random() * 100), Math.floor(Math.random() * 2), `这是随机的评论--${Math.ceil(Math.random() * 1000)}`),new ListItemClass(`小火车况且况且-${Math.ceil(Math.random() * 10)}`, Math.ceil(Math.random() * 100), Math.floor(Math.random() * 2), `这是随机的评论--${Math.ceil(Math.random() * 1000)}`),new ListItemClass(`小火车况且况且-${Math.ceil(Math.random() * 10)}`, Math.ceil(Math.random() * 100), Math.floor(Math.random() * 2), `这是随机的评论--${Math.ceil(Math.random() * 1000)}`),new ListItemClass(`小火车况且况且-${Math.ceil(Math.random() * 10)}`, Math.ceil(Math.random() * 100), Math.floor(Math.random() * 2), `这是随机的评论--${Math.ceil(Math.random() * 1000)}`)]
}// 子组件
@Component
struct ChildCom {@ObjectLink listInfo: ListItemClassbuild() {Column() {Text(this.listInfo.name).fontSize(18).fontWeight(700).fontColor('#333').margin({bottom: 10})Row() {Text(this.listInfo.comment)Row() {Text(this.listInfo.likeNum.toString()).fontColor(this.listInfo.isLike ? Color.Red : '#333')}.onClick(() => {if (this.listInfo.isLike) {this.listInfo.likeNum -= 1} else {this.listInfo.likeNum += 1}this.listInfo.isLike = !this.listInfo.isLike})}.justifyContent(FlexAlign.SpaceBetween).width('100%')}.padding(10).borderRadius(10).alignItems(HorizontalAlign.Start).width('100%').backgroundColor(Color.White)}
}// 父组件
@Entry
@Component
struct ObservedObjectLink {@State list: ListItemClass[] = createData()build() {Column() {List({space: 10}) {ForEach(this.list, (item: ListItemClass, index: number) => {ListItem() {ChildCom({listInfo: item})}})}}.padding(10).width('100%').height('100%').backgroundColor(Color.Gray)}
}

在这里插入图片描述

相关文章:

  • ls命令的参数选项
  • 【初阶数据结构】二叉树(附题)
  • 大厂笔试真题讲解—京东23—夹吃棋
  • 线性代数|机器学习-P15矩阵A的低秩变换下的逆矩阵
  • Java中的Socket编程详解
  • 利用nodejs实现图片上传后端,并实现回显
  • 内存优化技巧:让数据处理更高效
  • 【数据结构】排序(下)
  • 前端基础操作1——利用nvm任意切换(管理)node版本
  • Nuxt快速学习开发 - Nuxt3静态资源Assets
  • Vue3 + Ant-Design 中 a-date-picke 实现选择切换年份 没有鼠标光标,输入框内自带‘年’
  • leetcode27移除元素
  • 无版权图片素材搜索网站,解决无版权图片查找问题
  • 逆向学习 MFC 篇:视图分割和在 C++ 的 Windows 窗口程序中添加图标的方法
  • [贪心算法]忍者道具
  • 【从零开始安装kubernetes-1.7.3】2.flannel、docker以及Harbor的配置以及作用
  • 【翻译】babel对TC39装饰器草案的实现
  • 002-读书笔记-JavaScript高级程序设计 在HTML中使用JavaScript
  • Angular数据绑定机制
  • ES6 学习笔记(一)let,const和解构赋值
  • hadoop集群管理系统搭建规划说明
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • mongo索引构建
  • mysql_config not found
  • Vue UI框架库开发介绍
  • vue数据传递--我有特殊的实现技巧
  • 构造函数(constructor)与原型链(prototype)关系
  • 鱼骨图 - 如何绘制?
  • 栈实现走出迷宫(C++)
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • $LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
  • $NOIp2018$劝退记
  • %@ page import=%的用法
  • ()、[]、{}、(())、[[]]命令替换
  • (14)学习笔记:动手深度学习(Pytorch神经网络基础)
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (Mac上)使用Python进行matplotlib 画图时,中文显示不出来
  • (NO.00004)iOS实现打砖块游戏(十二):伸缩自如,我是如意金箍棒(上)!
  • (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
  • (SpringBoot)第七章:SpringBoot日志文件
  • (论文阅读40-45)图像描述1
  • (十八)Flink CEP 详解
  • (十八)三元表达式和列表解析
  • (转)利用ant在Mac 下自动化打包签名Android程序
  • (转)拼包函数及网络封包的异常处理(含代码)
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • .net6 core Worker Service项目,使用Exchange Web Services (EWS) 分页获取电子邮件收件箱列表,邮件信息字段
  • .Net--CLS,CTS,CLI,BCL,FCL
  • .net开源工作流引擎ccflow表单数据返回值Pop分组模式和表格模式对比
  • .NET企业级应用架构设计系列之应用服务器
  • .NET未来路在何方?
  • @TableId注解详细介绍 mybaits 实体类主键注解
  • [AIGC] 开源流程引擎哪个好,如何选型?
  • [ARC066F]Contest with Drinks Hard
  • [C][数据结构][树]详细讲解