HarmonyOS父子组件传递参数
创始人
2024-11-05 18:37:08
0

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: string   likeNum: number   isLike: boolean   comment: string    constructor(name: string, likeNum: number, isLike: number, comment: string) {     this.name = name     this.likeNum = likeNum     this.isLike = isLike === 0 ? false : true     this.comment = comment   } }   // 这是 孙子组件 @Component struct SunziCom {   // 注意:这里的属性名要保持和 @Provide修饰的父组件属性名一致.   @Consume listInfo: ListItemClass   build() {     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: string   likeNum: number   isLike: boolean   comment: string    constructor(name: string, likeNum: number, isLike: number, comment: string) {     this.name = name     this.likeNum = likeNum     this.isLike = isLike === 0 ? false : true     this.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: ListItemClass    build() {      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)   } } 

在这里插入图片描述

相关内容

热门资讯

微信怎样开金房间卡/微信链接斗... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享受...
科技实测!牛牛房卡游戏代理星云... 微信游戏中心:星云大厅房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程...
推荐一款!金花房卡是正规的高朋... 推荐一款!金花房卡是正规的高朋联盟/房卡正版如何购买Sa9Ix苹果iPhone 17手机即将进入量产...
正规平台有哪些,金花充值房卡趣... 您好!微信趣游联盟大厅链接获取房卡可以通过以下几种方式购买: 1.微信渠道:(趣游联盟)大厅介绍:...
秒懂教程!微信的炸金花房卡怎么... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:66336574许多玩家在游戏中会购买房卡来享...
玩家攻略,牛牛房卡游戏代理兄弟... 兄弟大厅/新道游房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 ...
微信链接炸金花房卡在哪买的/微... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
重大通报,牛牛充值房卡超凡联盟... 重大通报,牛牛充值房卡超凡联盟/微信链接房卡卖家联系方式超凡联盟是一款非常受欢迎的游戏,咨询房/卡添...
秒懂教程!微信拼三张怎么买房卡... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:56001354许多玩家在游戏中会购买房卡来享...
微信买链接拼三张房卡/毛豆大厅... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
正版授权!金花房卡怎么购买青龙... 青龙大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332900...
一分钟了解!金花房卡出售新蜜瓜... 一分钟了解!金花房卡出售新蜜瓜大厅/上游房卡多少钱一张Sa9Ix苹果iPhone 17手机即将进入量...
玩家攻略,牛牛房卡批发平台芙蓉... 今 日消息,芙蓉大厅房卡添加微信33549083 苹果今日发布了 iOS 16.1 正式版更新,简单...
秒懂教程!微信牛牛房间怎么弄,... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:71319951许多玩家在游戏中会购买房卡来享受...
科技实测!金花房卡官网荣耀联盟... 荣耀联盟房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 3、根...
微信拼三张在哪里充值房卡/新星... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
正版授权!微信金花房卡怎么弄悟... 正版授权!微信金花房卡怎么弄悟空系列/随意玩/房卡在哪里购买悟空系列/随意玩是一款非常受欢迎的游戏,...
玩家攻略,牛牛房卡批发平台昆仑... 微信游戏中心:昆仑大厅房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程...
一分钟了解!游戏微信牛牛房卡新... 新大圣/大圣大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:33...
ia攻略/金花房卡怎么购买白虎... 今 日消息,白虎大厅房卡添加微信33549083 苹果今日发布了 iOS 16.1 正式版更新,简单...