HarmonyOS 关于@BuilderParam UI没有更新的问题?

将@Builder 方法传递给listItem的子布局,然后改变item的数据,listitem 没更新

阅读 389
1 个回答

因为这样没有监听到变化,所以没有改变值,具体参考下对象数组的用法

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-V5\#对象数组

import { Child } from './Child'
import { Tmp } from './Tmp'

@Entry
@Component
struct Index {
  @State listData: Tmp[] = []

  @Builder
  componentBuilder($$: Tmp) {
    Text(`${$$.label}`)
  }

  build() {
    Column() {

      List() {
        ForEach(this.listData, (item: Tmp, idnex) => {
          ListItem() {
            Child({
              tmp: item, customOverBuilderParam: ($$): void => {
                this.componentBuilder({ label: $$.label })
              }
            })
          }

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

  aboutToAppear(): void {
    for (let index = 0; index < 10; index++) {
      let item = new Tmp()
      item.label = index + ""
      this.listData.push(item)
    }

    setTimeout(() => {
      let item = new Tmp()
      item.label = "bbbb"
      this.listData[1] = item
    }, 3000)
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进