HarmonyOS 有没类似BuilderParam的功能,但又可以传递参数的实现方式?

我在自定义组件里面拿到数据ForEach时, 子组件需要给外面去实现?而且需要根据数据变化而变化,使用BuilderParam发现无法传递参数,需要怎样实现?

譬如:

@BuilderParam itemBuilderParam: (item: CommonType, index: number,selectedIndex:number) => void = this.itemBuilder;

Row() {
  ForEach(this.tabDatas, (item: HomePageTopTabBean, index: number) => {
    this.itemBuilderParam
  })
}

这个BuilderParam 无法传递参数。

阅读 448
1 个回答

请参考如下代码

interface Inf {
  sss: string;
}

@Component
struct Test {
  @BuilderParam test: (inf: Inf) => void;
  @State str: string = "aaa"

  build() {
    Stack() {
      this.test({ sss: this.str })
      Button("测试")
        .onClick(() => {
          this.str = "bbb";
        })
    }

  }

}

@Entry
@Component
struct Index {
  build() {
    Stack() {
      Test({
        test: this.test
      })
    }
  }

  @Builder
  test(inf: Inf) {
    Text(inf.sss)
      .margin({top:100})
  }
}

主要是this 指向的问题,详情请参看文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-builderparam-V5\#初始化builderparam装饰的方法

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进