HarmonyOS Builder组件嵌套调用时,每个Builder都需要包一层引用才能刷新,有没有更好的办法?

咨询描述:

// fail 问题:无法展示(因为没有wrapper),点击后也无法刷新。

@Builder  
function BuildMyDataInnerInner(inner: MyDataInnerInner) {  
  if (inner instanceof MyDataInnerInner) {  
    Text(inner.msg + ' ').width('100%').height('100%')  
  }else {  
    // 其他判断逻辑  
  }  
}  
  
@Builder  
function BuildMyDataInner(inner: MyDataInner) {  
  if (inner instanceof MyDataInner) {  
    BuildMyDataInnerInner(inner.inner)  
  }else {  
    // 其他判断逻辑  
  }  
}  
  
@Builder  
function BuildMyData(wrapper: IDataWrapper) {  
  if (wrapper instanceof MyData) {  
    BuildMyDataInner(wrapper.inner)  
  }else {  
    // 其他判断逻辑  
  }  
}  
  
@Component  
export struct TestParentParent {  
  private name: string = "TestParentParent";  
  
  build() {  
    TestParent({  
      outerBuilder: BuildMyData  
    })  
  }  
}  
  
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;  
  }  
}  
  
  
@Component  
struct TestParent {  
  @BuilderParam outerBuilder: (wrapper: IDataWrapper) => void  
  
  build() {  
    TestChild({  
      outerBuilder: this.outerBuilder  
    })  
  }  
}  
  
  
interface IDataWrapper {  
  data: Object  
}  
  
  
@Component  
struct TestChild {  
  @State data: Object = new MyData(new MyDataInner(new MyDataInnerInner(111)));  
  @BuilderParam outerBuilder: (wrapper: IDataWrapper) => void  
  
  build() {  
    Stack() {  
      this.outerBuilder({  
        data: this.data  
      })  
      Button("Click").onClick(() => {  
        this.data = new MyData(new MyDataInner(new MyDataInnerInner(222)));  
      })  
    }.width('100%').height('100%')  
  }  
}  
// success 问题:可以展示,点击后也可以刷新,但每个builder都要包一层,过于繁琐  
@Builder  
function BuildMyDataInnerInner(inner: IDataWrapper<MyDataInnerInner>) {  
  if (inner.data instanceof MyDataInnerInner) {  
    Text(inner.data.msg + ' ').width('100%').height('100%')  
  }else {  
    // 其他判断逻辑  
  }  
}  
  
@Builder  
function BuildMyDataInner(innerWrapper: IDataWrapper<MyDataInner>) {  
  if (innerWrapper.data instanceof MyDataInner) {  
    BuildMyDataInnerInner({  
      data: innerWrapper.data.inner  
    })  
  }else {  
    // 其他判断逻辑  
  }  
}  
  
@Builder  
function BuildMyData(wrapper: IDataWrapper<Object>) {  
  if (wrapper.data instanceof MyData) {  
    BuildMyDataInner({  
      data: wrapper.data.inner  
    })  
  }else {  
    // 其他判断逻辑  
  }  
}  
  
@Component  
export struct TestParentParent {  
  private name: string = "TestParentParent";  
  
  build() {  
    TestParent({  
      outerBuilder: BuildMyData  
    })  
  }  
}  
  
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;  
  }  
}  
  
  
@Component  
struct TestParent {  
  @BuilderParam outerBuilder: (wrapper: IDataWrapper<Object>) => void  
  
  build() {  
    TestChild({  
      outerBuilder: this.outerBuilder  
    })  
  }  
}  
  
  
interface IDataWrapper<T> {  
  data: T  
}  
  
  
@Component  
struct TestChild {  
  @State data: Object = new MyData(new MyDataInner(new MyDataInnerInner(111)));  
  @BuilderParam outerBuilder: (wrapper: IDataWrapper<Object>) => void  
  
  build() {  
    Stack() {  
      this.outerBuilder({  
        data: this.data  
      })  
      Button("Click").onClick(() => {  
        this.data = new MyData(new MyDataInner(new MyDataInnerInner(222)));  
      })  
    }.width('100%').height('100%')  
  }  
}
HarmonyOS
2024-10-24 12:44:25
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

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
微信
回复
2024-10-24 15:09:16
相关问题
@Builder使用引用时异常
800浏览 • 1回复 待解决
HarmonyOS 如何在builder函数中传入组件
221浏览 • 0回复 待解决
@Extend、@Styles、@Builder 区别?
329浏览 • 1回复 待解决
HarmonyOS 有没有办法动态添加组件
254浏览 • 1回复 待解决
订阅emitter分享,谁有更好办法
630浏览 • 1回复 待解决
有没有什么办法组件新增方法?
678浏览 • 1回复 待解决
如何将点击事件透传到下一层
1806浏览 • 1回复 待解决
有没有调用日历接口?
6240浏览 • 1回复 待解决