#鸿蒙通关秘籍#如何通过@Prop装饰器实现HarmonyOS中父子组件单向数据流?

HarmonyOS
2024-11-27 12:55:24
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
CodeCyclops

@Prop装饰器允许子组件从父组件中接收数据,形成单向的数据流。父组件可以通过@Prop传递变量给子组件,子组件只能读取而不能影响父组件的状态。示例如下:

// 父组件
@Component
struct ParentComponent {
  @State parentValue: string = 'initial value'

  build() {
    ChildComponent({ propValue: this.parentValue })
  }
}

// 子组件
@Component
export struct ChildComponent {
  @Prop propValue: string

  build() {
    Text(this.propValue)
      .onClick(() => {
        console.log('prop cannot change parentValue')
      })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
分享
微博
QQ
微信
回复
2024-11-27 13:55:04


相关问题
HarmonyOS 如何解压数据流
810浏览 • 1回复 待解决
使用http模块实现数据流请求
1654浏览 • 1回复 待解决
HarmonyOS 解压gzip格式压缩的数据流
482浏览 • 1回复 待解决
实现文件解压缩和数据流解压缩
1913浏览 • 1回复 待解决