HarmonyOS @Prop同步给子组件的数据如何通过@Builder传递?

以下demo代码中,在父组件中调用TestBuilder,传递一个string类型数据,通过TestBuilder再传递给TestComp,并更新TestComp中的UI,但实际无法刷新UI。请问中间有个builder透传的场景,如何传递数据并更新UI?

@Component
struct TestComp {
  @Prop text: string = "init"
  build() {
    Text(this.text)

  }
}

@Builder
function TestBuilder(text: string) {
  Column() {
    TestComp({ text: text })
  }
}

@Entry
@Component
struct BuilderUpdateData {
  @State message: string = 'Hello World';

  build() {
    Column() {
      TestBuilder(this.message)
      // TestComp({ text: this.message })  // 如果父组件直接调TestComp就可以刷新UI
      Button('update message', { stateEffect: true, type: ButtonType.Capsule })
        .width('80%')
        .height(40)
        .margin(10)
        .onClick(() => {
          this.message = "aaa"
        })

    }
    .height('100%')
    .width('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
HarmonyOS
2024-12-24 17:31:21
1199浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

需按引用传递参数,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-builder-V5#按引用传递参数

示例:

class Tmp {
  para: string = ""
}

@Component
struct TestComp {
  @Prop text: string = "1";
  build() {
    Text(this.text)
  }
}

@Builder
function TestBuilder($$ : Tmp) {
  Column() {
    TestComp({text: $$.para})
  }
}

@Entry
@Component
struct BuilderUpdateData {
  @State message: string = 'Hello World';

  build() {
    Column() {
      TestBuilder({para: this.message})
      // TestComp({ text: this.message }) // 如果父组件直接调TestComp就可以刷新UI
      Button('update message', { stateEffect: true, type: ButtonType.Capsule })
        .width('80%')
        .height(40)
        .margin(10)
        .onClick(() => {
          this.message = "aaa"
        })

    }
    .height('100%')
    .width('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
分享
微博
QQ
微信
回复
2024-12-24 20:19:52


相关问题
组件组件传递函数
996浏览 • 1回复 待解决
HarmonyOS builder 作为 builder 参数传递
911浏览 • 1回复 待解决
HarmonyOS @Prop参数传递问题
762浏览 • 1回复 待解决
HarmonyOS @builder引用传递问题
1278浏览 • 0回复 待解决
HarmonyOS 如何方法加同步
690浏览 • 0回复 待解决
HarmonyOS 通过属性来传递组件
562浏览 • 1回复 待解决
HarmonyOS 能否通过eventHub传递Want数据
597浏览 • 1回复 待解决