@BuilderParam裝飾器:引用@Builder函數(shù)

2024-01-25 12:01 更新

當開發(fā)者創(chuàng)建了自定義組件,并想對該組件添加特定功能時,例如在自定義組件中添加一個點擊跳轉(zhuǎn)操作。若直接在組件內(nèi)嵌入事件方法,將會導致所有引入該自定義組件的地方均增加了該功能。為解決此問題,ArkUI引入了@BuilderParam裝飾器,@BuilderParam用來裝飾指向@Builder方法的變量,開發(fā)者可在初始化自定義組件時對此屬性進行賦值,為自定義組件增加特定的功能。該裝飾器用于聲明任意UI描述的一個元素,類似slot占位符。

說明

從API version 9開始,該裝飾器支持在ArkTS卡片中使用。

裝飾器使用說明

初始化@BuilderParam裝飾的方法

@BuilderParam裝飾的方法只能被自定義構(gòu)建函數(shù)(@Builder裝飾的方法)初始化。

  • 使用所屬自定義組件的自定義構(gòu)建函數(shù)或者全局的自定義構(gòu)建函數(shù),在本地初始化@BuilderParam。
    1. @Builder function GlobalBuilder0() {}
    2. @Component
    3. struct Child {
    4. @Builder doNothingBuilder() {};
    5. @BuilderParam aBuilder0: () => void = this.doNothingBuilder;
    6. @BuilderParam aBuilder1: () => void = GlobalBuilder0;
    7. build(){}
    8. }
  • 用父組件自定義構(gòu)建函數(shù)初始化子組件@BuilderParam裝飾的方法。
    1. @Component
    2. struct Child {
    3. @BuilderParam aBuilder0: () => void;
    4. build() {
    5. Column() {
    6. this.aBuilder0()
    7. }
    8. }
    9. }
    10. @Entry
    11. @Component
    12. struct Parent {
    13. @Builder componentBuilder() {
    14. Text(`Parent builder `)
    15. }
    16. build() {
    17. Column() {
    18. Child({ aBuilder0: this.componentBuilder })
    19. }
    20. }
    21. }

  • 需注意this指向正確。
    以下示例中,Parent組件在調(diào)用this.componentBuilder()時,this指向其所屬組件,即“Parent”。@Builder componentBuilder()傳給子組件@BuilderParam aBuilder0,在Child組件中調(diào)用this.aBuilder0()時,this指向在Child的label,即“Child”。
    說明

    開發(fā)者謹慎使用bind改變函數(shù)調(diào)用的上下文,可能會使this指向混亂。

    1. @Component
    2. struct Child {
    3. label: string = `Child`
    4. @BuilderParam aBuilder0: () => void;
    5. build() {
    6. Column() {
    7. this.aBuilder0()
    8. }
    9. }
    10. }
    11. @Entry
    12. @Component
    13. struct Parent {
    14. label: string = `Parent`
    15. @Builder componentBuilder() {
    16. Text(`${this.label}`)
    17. }
    18. build() {
    19. Column() {
    20. this.componentBuilder()
    21. Child({ aBuilder0: this.componentBuilder })
    22. }
    23. }
    24. }

使用場景

參數(shù)初始化組件

@BuilderParam裝飾的方法可以是有參數(shù)和無參數(shù)的兩種形式,需與指向的@Builder方法類型匹配。@BuilderParam裝飾的方法類型需要和@Builder方法類型一致。

  1. @Builder function GlobalBuilder1($$ : {label: string }) {
  2. Text($$.label)
  3. .width(400)
  4. .height(50)
  5. .backgroundColor(Color.Green)
  6. }
  7. @Component
  8. struct Child {
  9. label: string = 'Child'
  10. // 無參數(shù)類,指向的componentBuilder也是無參數(shù)類型
  11. @BuilderParam aBuilder0: () => void;
  12. // 有參數(shù)類型,指向的GlobalBuilder1也是有參數(shù)類型的方法
  13. @BuilderParam aBuilder1: ($$ : { label : string}) => void;
  14. build() {
  15. Column() {
  16. this.aBuilder0()
  17. this.aBuilder1({label: 'global Builder label' } )
  18. }
  19. }
  20. }
  21. @Entry
  22. @Component
  23. struct Parent {
  24. label: string = 'Parent'
  25. @Builder componentBuilder() {
  26. Text(`${this.label}`)
  27. }
  28. build() {
  29. Column() {
  30. this.componentBuilder()
  31. Child({ aBuilder0: this.componentBuilder, aBuilder1: GlobalBuilder1 })
  32. }
  33. }
  34. }

尾隨閉包初始化組件

在自定義組件中使用@BuilderParam裝飾的屬性時也可通過尾隨閉包進行初始化。在初始化自定義組件時,組件后緊跟一個大括號“{}”形成尾隨閉包場景。

說明

此場景下自定義組件內(nèi)有且僅有一個使用@BuilderParam裝飾的屬性。

開發(fā)者可以將尾隨閉包內(nèi)的內(nèi)容看做@Builder裝飾的函數(shù)傳給@BuilderParam。示例如下:

  1. // xxx.ets
  2. @Component
  3. struct CustomContainer {
  4. @Prop header: string;
  5. @BuilderParam closer: () => void
  6. build() {
  7. Column() {
  8. Text(this.header)
  9. .fontSize(30)
  10. this.closer()
  11. }
  12. }
  13. }
  14. @Builder function specificParam(label1: string, label2: string) {
  15. Column() {
  16. Text(label1)
  17. .fontSize(30)
  18. Text(label2)
  19. .fontSize(30)
  20. }
  21. }
  22. @Entry
  23. @Component
  24. struct CustomContainerUser {
  25. @State text: string = 'header';
  26. build() {
  27. Column() {
  28. // 創(chuàng)建CustomContainer,在創(chuàng)建CustomContainer時,通過其后緊跟一個大括號“{}”形成尾隨閉包
  29. // 作為傳遞給子組件CustomContainer @BuilderParam closer: () => void的參數(shù)
  30. CustomContainer({ header: this.text }) {
  31. Column() {
  32. specificParam('testA', 'testB')
  33. }.backgroundColor(Color.Yellow)
  34. .onClick(() => {
  35. this.text = 'changeHeader';
  36. })
  37. }
  38. }
  39. }
  40. }

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號