如何把一个@Component struct定义的组件,通过参数传递到另一个组件中去?

@BuilderParam 好像只可以传入@build修饰的组件,但这个不满足我的需求。

HarmonyOS
2024-10-11 11:33:22
1151浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

@BuilderParam装饰的方法只能被自定义构建函数(@Builder装饰的方法)初始化,可以将@Component struct定义的组件用@Builder装饰

参考demo:

import { ErZi, SunZi } from "./TestComponent"  
  
@Entry  
@Component  
struct Index {  
  build() {  
    Column() {  
      ErZi({  
        customView: () => {  
          this.testSunZi()  
        }  
      })  
    }  
  }  
  @Builder  
  testSunZi() {  
    SunZi()  
  }  
}  
//testComponent.ets  
@Component  
export struct ErZi {  
  
  @BuilderParam customView?: () => void  
  build() {  
    Column() {  
      Text('ErZi')  
      if (this.customView) this.customView()  
    }  
  }  
}  
  
@Component  
export struct SunZi {  
  build() {  
    Column() {  
      Text('SunZi')  
    }  
  }  
}
  • 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.
分享
微博
QQ
微信
回复
2024-10-11 17:33:02
相关问题
如何一个Component画到Pixelmap上
2571浏览 • 1回复 待解决
如何一个Component画到Pixelmap上呢?
3616浏览 • 1回复 待解决
entry中如何拉起另一个Module中Ability
5158浏览 • 1回复 待解决