鸿蒙HarmonyOSArkUI(eTS)组件通信方式总结之
鸿蒙HarmonyOSArkUI(eTS)组件间通信涉及组件属性与显示、父子组件间通信、祖孙组件间通信、不相干组件间通信等,而组件两两间通信也有单向与双向之分。通过学习HDC2021和官方文档,本系列以State、Link、Prop、Provide与Consume、StorageLink等组件状态装饰器介绍组件间通信方式。
本次介绍:祖孙组件间通信方式之一State、Link、Prop。
1。父组件State,子组件Link,孙组件Link
数据流向是:父组件子组件孙组件EntryComponentstructIndex{StatefatherVal:number0build(){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)}}子组件ComponentstructsubIndex{LinksonVal:numberbuild(){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)}}孙组件ComponentstructgrandsonIndex{LinkgrandsonVal:numberbuild(){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
数据流向是:父组件子组件孙组件EntryComponentstructIndex{StatefatherVal:number0build(){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)}}子组件ComponentstructsubIndex{PropsonVal:numberbuild(){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)}}孙组件ComponentstructgrandsonIndex{PropgrandsonVal:numberbuild(){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
数据流向是:父组件子组件孙组件EntryComponentstructIndex{StatefatherVal:number0build(){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)}}子组件ComponentstructsubIndex{LinksonVal:numberbuild(){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)}}孙组件ComponentstructgrandsonIndex{PropgrandsonVal:numberbuild(){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装饰的变量。如果想达到以下数据流向:父组件子组件孙组件,可采用以下方式实现:EntryComponentstructIndex{fatherVal:number6StatefatherVal2:numberthis。fatherValbuild(){Column(){Text(父组件:{this。fatherVal2})。fontSize(50)。fontWeight(FontWeight。Bold)。margin({top:30})Row(){Button(加1)。width(100)。onClick((){this。fatherVal2this。fatherVal})。margin({right:20})Button(减1)。width(100)。onClick((){this。fatherVal2this。fatherVal})}。margin({bottom:20})Divider()subIndex({sonVal:this。fatherVal})}。width(100)。height(100)。backgroundColor(Color。Pink)}}子组件ComponentstructsubIndex{StatesonVal:number0build(){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)}}孙组件ComponentstructgrandsonIndex{LinkgrandsonVal:numberbuild(){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)}}