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 }))
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

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

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

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

@builder
function builderB(params: paramsB) {

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

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

HarmonyOS
2024-12-25 11:37:43
浏览
收藏 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;
  }
}
  • 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.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
分享
微博
QQ
微信
回复
2024-12-25 14:25:15
相关问题
HarmonyOS @builder引用传递问题
1212浏览 • 0回复 待解决
HarmonyOS 想在builder中插入builder
587浏览 • 1回复 待解决
HarmonyOS 怎么把组件作为参数传递
996浏览 • 1回复 待解决
HarmonyOS 对象作为参数传递时方法丢失
1193浏览 • 1回复 待解决
HarmonyOS 如何使用全局Builder
600浏览 • 1回复 待解决
HarmonyOS @Builder内容如何刷新
600浏览 • 1回复 待解决
@Extend、@Styles、@Builder 区别?
955浏览 • 1回复 待解决