鸿蒙HarmonyOSArkUI(eTS)组件通信方式总结之四祖孙组件间1
鸿蒙HarmonyOS ArkUI(eTS)组件间通信涉及组件属性与显示、父子组件间通信、祖孙组件间通信、不相干组件间通信等,而组件两两间通信也有单向与双向之分。通过学习HDC2021和官方文档,本系列以@State、@Link、@Prop、@Provide与@Consume、@StorageLink等组件状态装饰器介绍组件间通信方式。
本次介绍:祖孙组件间通信方式之一@State、@Link、@Prop。
1.父组件@State,子组件@Link,孙组件@Link
数据流向是: 父组件 <--> 子组件 <--> 孙组件 @Entry @Component struct Index { @State fatherVal: number = 0 build() { Column(){ Text(`父组件: ${this.fatherVal}`) .fontSize(50) .fontWeight(FontWeight.Bold) .margin({top: 30}) Row(){ Button("加 1") .width("100") .onClick(()=> { this.fatherVal ++ }) .margin({right:20}) Button("减 1") .width("100") .onClick(()=> { this.fatherVal -- }) } .margin({bottom: 20}) Divider() subIndex({sonVal: $fatherVal}) } .width("100%") .height("100%") .backgroundColor(Color.Pink) } } //子组件 @Component struct subIndex { @Link sonVal: number build(){ Column(){ Text(`子组件: ${this.sonVal}`) .fontSize(40) .fontWeight(FontWeight.Bold) .margin({top: 30}) Row(){ Button("加 1") .width("100") .onClick(()=> { this.sonVal ++ }) .margin({right:20}) Button("减 1") .width("100") .onClick(()=> { this.sonVal -- }) } .margin({bottom: 20}) Divider() grandsonIndex({grandsonVal: $sonVal}) } .width("90%") .backgroundColor(Color.Orange) } } //孙组件 @Component struct grandsonIndex { @Link grandsonVal: number build(){ Column(){ Text(`孙组件: ${this.grandsonVal}`) .fontSize(30) .fontWeight(FontWeight.Bold) .margin({top: 30}) Row(){ Button("加 1") .width("100") .onClick(()=> { this.grandsonVal ++ }) .margin({right:20}) Button("减 1") .width("100") .onClick(()=> { this.grandsonVal -- }) } } .width("80%") .backgroundColor(Color.Brown) } }
2.父组件@State,子组件@Prop,孙组件@Prop
数据流向是:父组件 --> 子组件 --> 孙组件 @Entry @Component struct Index { @State fatherVal: number = 0 build() { Column(){ Text(`父组件: ${this.fatherVal}`) .fontSize(50) .fontWeight(FontWeight.Bold) .margin({top: 30}) Row(){ Button("加 1") .width("100") .onClick(()=> { this.fatherVal ++ }) .margin({right:20}) Button("减 1") .width("100") .onClick(()=> { this.fatherVal -- }) } .margin({bottom: 20}) Divider() subIndex({sonVal: this.fatherVal}) } .width("100%") .height("100%") .backgroundColor(Color.Pink) } } //子组件 @Component struct subIndex { @Prop sonVal: number build(){ Column(){ Text(`子组件: ${this.sonVal}`) .fontSize(40) .fontWeight(FontWeight.Bold) .margin({top: 30}) Row(){ Button("加 1") .width("100") .onClick(()=> { this.sonVal ++ }) .margin({right:20}) Button("减 1") .width("100") .onClick(()=> { this.sonVal -- }) } .margin({bottom: 20}) Divider() grandsonIndex({grandsonVal: this.sonVal}) } .width("90%") .backgroundColor(Color.Orange) } } //孙组件 @Component struct grandsonIndex { @Prop grandsonVal: number build(){ Column(){ Text(`孙组件: ${this.grandsonVal}`) .fontSize(30) .fontWeight(FontWeight.Bold) .margin({top: 30}) Row(){ Button("加 1") .width("100") .onClick(()=> { this.grandsonVal ++ }) .margin({right:20}) Button("减 1") .width("100") .onClick(()=> { this.grandsonVal -- }) } } .width("80%") .backgroundColor(Color.Brown) } }
3.父组件@State,子组件@Link,孙组件@Prop
数据流向是:父组件 <--> 子组件 --> 孙组件 @Entry @Component struct Index { @State fatherVal: number = 0 build() { Column(){ Text(`父组件: ${this.fatherVal}`) .fontSize(50) .fontWeight(FontWeight.Bold) .margin({top: 30}) Row(){ Button("加 1") .width("100") .onClick(()=> { this.fatherVal ++ }) .margin({right:20}) Button("减 1") .width("100") .onClick(()=> { this.fatherVal -- }) } .margin({bottom: 20}) Divider() subIndex({sonVal: $fatherVal}) } .width("100%") .height("100%") .backgroundColor(Color.Pink) } } //子组件 @Component struct subIndex { @Link sonVal: number build(){ Column(){ Text(`子组件: ${this.sonVal}`) .fontSize(40) .fontWeight(FontWeight.Bold) .margin({top: 30}) Row(){ Button("加 1") .width("100") .onClick(()=> { this.sonVal ++ }) .margin({right:20}) Button("减 1") .width("100") .onClick(()=> { this.sonVal -- }) } .margin({bottom: 20}) Divider() grandsonIndex({grandsonVal: this.sonVal}) } .width("90%") .backgroundColor(Color.Orange) } } //孙组件 @Component struct grandsonIndex { @Prop grandsonVal: number build(){ Column(){ Text(`孙组件: ${this.grandsonVal}`) .fontSize(30) .fontWeight(FontWeight.Bold) .margin({top: 30}) Row(){ Button("加 1") .width("100") .onClick(()=> { this.grandsonVal ++ }) .margin({right:20}) Button("减 1") .width("100") .onClick(()=> { this.grandsonVal -- }) } } .width("80%") .backgroundColor(Color.Brown) } }
不存在父组件@State、子组件@Prop、孙组件@Link这种情况,因为子组件中@Prop装饰的变量不能赋给孙组件中@Link装饰的变量。如果想达到以下数据流向:父组件 --> 子组件 <--> 孙组件,可采用以下方式实现: @Entry @Component struct Index { fatherVal: number = 6 @State fatherVal2: number = this.fatherVal build() { Column(){ Text(`父组件: ${this.fatherVal2}`) .fontSize(50) .fontWeight(FontWeight.Bold) .margin({top: 30}) Row(){ Button("加 1") .width("100") .onClick(()=> { this.fatherVal2 ++ this.fatherVal ++ }) .margin({right:20}) Button("减 1") .width("100") .onClick(()=> { this.fatherVal2 -- this.fatherVal -- }) } .margin({bottom: 20}) Divider() subIndex({sonVal: this.fatherVal}) } .width("100%") .height("100%") .backgroundColor(Color.Pink) } } //子组件 @Component struct subIndex { @State sonVal: number = 0 build(){ Column(){ Text(`子组件: ${this.sonVal}`) .fontSize(40) .fontWeight(FontWeight.Bold) .margin({top: 30}) Row(){ Button("加 1") .width("100") .onClick(()=> { this.sonVal ++ }) .margin({right:20}) Button("减 1") .width("100") .onClick(()=> { this.sonVal -- }) } .margin({bottom: 20}) Divider() grandsonIndex({grandsonVal: $sonVal}) } .width("90%") .backgroundColor(Color.Orange) } } //孙组件 @Component struct grandsonIndex { @Link grandsonVal: number build(){ Column(){ Text(`孙组件: ${this.grandsonVal}`) .fontSize(30) .fontWeight(FontWeight.Bold) .margin({top: 30}) Row(){ Button("加 1") .width("100") .onClick(()=> { this.grandsonVal ++ }) .margin({right:20}) Button("减 1") .width("100") .onClick(()=> { this.grandsonVal -- }) } } .width("80%") .backgroundColor(Color.Brown) } }
面试官连这些问题都不知道?就想要50k?前言本人2016年毕业,目前从事Android开发工作已经3年啦。就是尘世间一个迷途小开发,二流程序员,居身于小城市,最终也是考虑到发展前景的局限性,趁着自己还年轻,于是毅然裸辞,
相互宝关停的背后编辑导语近日,相互宝宣布将于2022年关停,因为这一消息,业内关于互联网互助平台的发展又展开了新一轮的讨论。为什么互联网互助平台的发展总是容易遇到商业变现上的困难?本文作者结合自己
英特尔道歉,华为鸿蒙汽车问世!国产CPU的出路国产品牌一度崛起,我们的眼里只有国家的物品,国产我们一直都知道很好用,又很实惠。但是呢,总有一个搅屎棍英特尔。英特尔公司它要求供应商不要使用我们新疆的产品,英特尔这又是遭受了什么委
华为Mate50Pro开始发力华为作为相当优秀的国产手机品牌,因为强悍的实力,优秀的品质,深受国内外用户欢迎。如果没有被制裁,华为将成为全球最受欢迎的手机厂商,市场占有率将会大大提升,全面超越三星和苹果。不过事
望眼欲穿,iphoneSE3要来了,搭载A15处理器,支持5G网络自2020年4月15日发布iphoneSE2后,果粉们整整等了1年,iphoneSE3还是没有出现。苹果的SE系列机型机身小巧,价格实在,性能强悍,很受小屏爱好者的青睐。iPhon
36氪首发从SDWAN延展到安全运维业务,七云网络完成数千万元的A轮融资36氪获悉,SDWAN及网络运维解决方案服务商七云网络已于日前完成A轮融资。本轮融资金额在数千万元级别,由维思资本领投,老股东常春藤资本和炬成资本继续加码,毅仁资本担任独家财务顾问
判断是否装了QQ判断是否装了QQpublicstaticbooleanisQQClientAvailable(Contextcontext)finalPackageManagerpackageMa
打造2万亿电商平台,马云却预言新零售会替代电商,提前投资700亿现如今,随着懒人经济的崛起,网上购物已经成了大家买东西的主要方式,而之所以可以享受这一方式,主要就是靠着马云花费2万亿打造的电商平台。值得注意的是,近两年马云又将目光放到了新零售领
为什么很多人买手机只关注高端旗舰机?为什么有些人要买贵手机在买手机这件事上,买贵的手机是如何定义的,我觉得应该是这么理解,为什么很多人买手机只关注高端旗舰机。或者只看苹果手机,或者只看华为手机!因为这些手机价格都基本
网站如何识别爬虫来访作为网站管理者,本着对网站数据客观详实的原则,是需要把爬虫带来的问题及时修复掉的,那核心问题就是网站如何识别爬虫来访?识别方案获取网页请求的useragent信息,使用特定标识判断
互联网2021作者李好零九编辑钊岁末已至,是结束也是开始。2021年互联网圈的那些大事件,你还记得吗?反垄断社区团购广告行业增速放缓直播带货主播被罚刚送走庚子年的疫情,互联网行业还是没能继续大放