HarmonyOS builder 作为 builder 的参数传递

export interface CustomOperationOption {
  headerSlot?: WrappedBuilder<object[]>
}

@Builder
export function OperationPanel(params: InternalBuilderParams<CustomOperationOption>) {
  Column() {
    if (params.customOption.headerSlot) {

    }
  }
  .width('750lpx')
  .backgroundColor($r('app.color.fill_white'))
  .transition(TransitionEffect.translate({ y: '100%' }).animation({ duration: 300, curve: Curve.EaseInOut }))
}

以上代码,在使用 headerSlot 的时候报错,这种 builder 需要引用另外一个 builder(不定)的情况,需要怎么写呢。

new ComponentContent(uiContext, wrapBuilder(builderA), paramsA);

@builder
function builderA(params: paramsA) {
   Column() {
      builderB({ xxx })
   }
}

@builder
function builderB(params: paramsB) {

}

以上代码,builderB 里面结束到的参数是 paramsA,这就导致类型没法书写,这是在设计预期内的吗

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

Builder组件嵌套调用demo:

1.Builder调用Builder的时候,需要按照键值对的形式传入

2.Builder传参的时候需要使用$$

@Entry
@Component
export struct TestParentParent {
  @State data: MyData = new MyData(new MyDataInner(new MyDataInnerInner(111)));
  build() {
    Column(){
      Button("Click").onClick(() => {
        this.data = new MyData(new MyDataInner(new MyDataInnerInner(222)));
      })
      BuildMyData({data: this.data})
      // TestParent({
      //   outerBuilder: BuildMyData
      // })
    }
  }
}

@Component
struct TestParent {
  // @BuilderParam outerBuilder: (wrapper: IDataWrapper) => void
  @Prop data: number = 0;

  build() {
    Row() {
      TestChild({
        data: this.data
      })
    }
  }
}

@Component
struct TestChild {
  @Prop data: number = 0;
  // @BuilderParam outerBuilder: (wrapper: IDataWrapper) => void

  build() {
    Row() {
      Text(this.data + ' ')
    }
    // this.outerBuilder(
    //   {data: this.data}
    // )
    // Button("Click").onClick(() => {
    //   // (this.data as MyData).inner.inner.msg = 222;
    //   this.data = new MyData(new MyDataInner(new MyDataInnerInner(222)));
    // })
  }
}
@Builder
function BuildMyDataInnerInner($$: IDataWrapper) {
  if ($$.data instanceof MyData) {
    TestParent({ data: $$.data.inner.inner.msg})
  }else {
    // 其他判断逻辑
  }
}

@Builder
function BuildMyDataInner($$: IDataWrapper) {
  if ($$.data instanceof MyData) {
    BuildMyDataInnerInner({ data: $$.data})
  }else {
    // 其他判断逻辑
  }
}

@Builder
function BuildMyData($$: IDataWrapper) {
  if ($$.data instanceof MyData) {
    Column() {
      BuildMyDataInner({ data: $$.data})
    }
  }else {
    // 其他判断逻辑
  }
}


interface IDataWrapper {
  data: Object
}

class MyDataInnerInner {
  msg: number;
  constructor(msg: number) {
    this.msg = msg;
  }
}

class MyDataInner {
  inner: MyDataInnerInner;
  constructor(inner: MyDataInnerInner) {
    this.inner = inner;
  }
}
/**
 * 需要自定义数据,包一层
 */
class MyData {
  inner: MyDataInner;
  constructor(inner: MyDataInner) {
    this.inner = inner;
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS @builder引用传递问题
56浏览 • 0回复 待解决
HarmonyOS 想在builder中插入builder
71浏览 • 1回复 待解决
HarmonyOS 怎么把组件作为参数传递
42浏览 • 1回复 待解决
HarmonyOS @Builder内容如何刷新
78浏览 • 1回复 待解决
HarmonyOS 如何使用全局Builder
0浏览 • 1回复 待解决
@Extend、@Styles、@Builder 区别?
472浏览 • 1回复 待解决
HarmonyOS @builder方法ui不刷新
59浏览 • 1回复 待解决
HarmonyOS @Builder UI刷新问题
36浏览 • 1回复 待解决