HarmonyOS Next ArkUI @State @Prop @Link @Provide @Consume笔记

学习目标:

@State装饰器
@Prop装饰器
@Link装饰器
@Link装饰器
@Provide装饰器
@Consume装饰器


学习内容:

GrandsonComponent

typescript 复制代码
@Component
export struct GrandsonComponent {

  @Consume('provideValue') consumeValue: number

  build() {
    Column(){
      Text('孙组件').fontSize(25)
      Text('consumeValue: ' + this.consumeValue)
      Button('Consume变量测试,父子兄弟都会变')
        .onClick(() => {
          this.consumeValue = Math.floor(Math.random() * (100))
        })
    }
  }
}

HelloComponent

typescript 复制代码
import {GrandsonComponent} from './GrandsonComponent'

@Component
export struct HelloComponent {

  @State stateValue: number = 0
  @Prop  propValue: number = 0
  @Link linkValue: number

  @Consume('provideValue') consumeValue: number

  build() {
    // HelloComponent自定义组件组合系统组件Row和Text
    Column({space:10}) {

      //父子之前双向同步
      Text('stateValue: '+this.stateValue.toString())

      //父->子单项同步
      Text('propValue: '+this.propValue.toString())

      Text('linkValue: '+this.linkValue.toString())

      Text('consumeValue: '+this.consumeValue.toString())

      Button('子State变量测试,只会当前组件变')
      .onClick(() => {
        this.stateValue = Math.floor(Math.random() * (100))
      })

      Button('子Prop变量测试,只会当前组件变')
      .onClick(() => {
        this.propValue = Math.floor(Math.random() * (100))
      })

      Button('子Link变量测试, 子变父也变')
      .onClick(() => {
        this.linkValue = Math.floor(Math.random() * (100));
      })

      GrandsonComponent()

    }.height('100%')
    .width('100%')
  }
}

ParentComponent

typescript 复制代码
import {HelloComponent} from './HelloComponent'
@Entry
@Component
struct ParentComponent {
  // 父组件的变化不会影响子子组件改变
  @State stateValue: number = 1
  // 父组件的变化会影响子子组件改变
  @State propValue: number = 2
  // 父子双向同步
  @State linkValue: number = 3

  @Provide provideValue: number = 5

  build() {

    Column({ space: 5 }) {
      Text('父组件').fontSize(25)
      Divider()
      //父子之前双向同步
      Text('stateValue: ' + this.stateValue.toString())

      //父->子单项同步
      Text('propValue: ' + this.propValue.toString())

      Text('linkValue: ' + this.linkValue.toString())

      Text('provideValue: ' + this.provideValue.toString())

      Button('父State变量测试,只会当前组件变')
      .onClick(() => {
        this.stateValue = Math.floor(Math.random() * (100));
      })

      Button('父Prop变量测试, 父变子也变')
      .onClick(() => {
        this.propValue = Math.floor(Math.random() * (100));
      })

      Button('父Link变量测试,父变子也变')
      .onClick(() => {
        this.linkValue = Math.floor(Math.random() * (100));
      })

      Button('父Provide变量测试,父子兄弟都会变')
        .onClick(() => {
          this.provideValue = Math.floor(Math.random() * (100));
        })

      Text('子组件').fontSize(25)
      Divider()

      HelloComponent({ stateValue: this.stateValue, propValue: this.propValue, linkValue: this.linkValue });

    }
    .height('100%')
    .width('100%')

  }
}

学习产出:

相关推荐
youtootech9 分钟前
HarmonyOS《柚兔学伴》项目实战09-待办列表 UI——List+ForEach+滑动删除
数据结构·ui·华为·list·harmonyos
不羁的木木19 分钟前
HarmonyOS APP实战-基于Image Kit的图像处理APP - 第6篇:图片滤镜效果实现
图像处理·深度学习·harmonyos
拥抱太阳06161 小时前
HarmonyOS 应用开发《掌上英语》第9篇—ArkTS @ObservedV2 + @Trace 响应式编程精讲
华为·harmonyos
拥抱太阳06161 小时前
HarmonyOS 应用开发《掌上英语》第10篇——@Local vs @Param vs @Trace 组件状态管理三剑客
华为·harmonyos
春卷同学8 小时前
HarmonyOS掌上记账APP开发实践第8篇:构建可测试的鸿蒙应用 — MoneyTrack 的自动化测试体系
华为·harmonyos
梦想不只是梦与想9 小时前
鸿蒙中declare关键字
harmonyos·declare
FrameNotWork9 小时前
HarmonyOS 6.0 输入法与键盘避让 — 聊天页面底部输入栏被键盘挡住的绝望
华为·计算机外设·harmonyos
念雨思9 小时前
信用卡权益对比:基于HarmonyOS + ArkTS的AI智能金融助手开发实践
人工智能·学习·华为·金融·harmonyos·鸿蒙
不羁的木木10 小时前
HarmonyOS APP实战-基于Image Kit的图像处理APP - 第12篇:性能优化与内存管理
图像处理·性能优化·harmonyos