06-鸿蒙4.0学习之后代组件数据双向Provide和Consume

06-鸿蒙4.0学习之后代组件数据双向Provide和Consume

代码

@Entry
@Component
struct ProvideUI {
  @State message: string = 'Hello World'
  // @Provide wechat: string = '我是祖父的值'
  @Provide('theshy') wechat: string = '我是祖父的值'


  build() {
    Row() {
      Column() {
        Text(this.wechat)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button('修改祖父的值')
          .onClick(() => {
            this.wechat = '1123'
          })
        Divider()
        CompA()
      }
      .width('100%')
    }
    .height('100%')
  }
}

@Component
struct CompA {
  build() {
    CompB()
  }
}

@Component
struct CompB {
  // @Consume wechat: string
  @Consume('theshy') stdy: string
  build() {
    Column() {
      Text(this.stdy)
      Button('修改孙子的值')
        .onClick(() => {
          this.stdy = '2211'
        })
    }
  }
}

你可能感兴趣的:(鸿蒙学习笔记整理,学习)