• OpenHarmony装饰指定自定义组件:@BuilderParam装饰器


    当开发者创建了自定义组件,并想对该组件添加特定功能时,例如在自定义组件中添加一个点击跳转操作。若直接在组件内嵌入事件方法,将会导致所有引入该自定义组件的地方均增加了该功能。为解决此问题,ArkUI引入了@BuilderParam装饰器,@BuilderParam用来装饰指向@Builder方法的变量,开发者可在初始化自定义组件时对此属性进行赋值,为自定义组件增加特定的功能。该装饰器用于声明任意UI描述的一个元素,类似slot占位符。

    说明:

    从API version 9开始,该装饰器支持在ArkTS卡片中使用。

    装饰器使用说明

    初始化@BuilderParam装饰的方法

    @BuildParam装饰的方法只能被自定义构建函数(@Builder装饰的方法)初始化。

    • 使用所属自定义组件的自定义构建函数或者全局的自定义构建函数,在本地初始化@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. }
    • 用父组件自定义构建函数初始化子组件@BuildParam装饰的方法。
    1. @Component
    2. struct Child {
    3.   @Builder componentBuilder() {
    4.     Text(`Parent builder `)
    5.   }
    6.   @BuilderParam aBuilder0: () => void = this.componentBuilder;
    7.   build() {
    8.     Column() {
    9.       this.aBuilder0()
    10.     }
    11.   }
    12. }
    13. @Entry
    14. @Component
    15. struct Parent {
    16.   @Builder componentBuilder() {
    17.     Text(`Parent builder `)
    18.   }
    19.   build() {
    20.     Column() {
    21.       Child({ aBuilder0: this.componentBuilder })
    22.     }
    23.   }
    24. }
    • 需注意this指向正确。

    以下示例中,Parent组件在调用this.componentBuilder()时,this.label指向其所属组件,即“Parent”。@Builder componentBuilder()传给子组件@BuilderParam aBuilder0,在Child组件中调用this.aBuilder0()时,this.label指向在Child的label,即“Child”。对于@BuilderParam aBuilder1,在将this.componentBuilder传给aBuilder1时,调用bind绑定了this,因此其this.label指向Parent的label。

    说明:

    开发者谨慎使用bind改变函数调用的上下文,可能会使this指向混乱。

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

    使用场景

    参数初始化组件

    @BuilderParam装饰的方法可以是有参数和无参数的两种形式,需与指向的@Builder方法类型匹配。@BuilderParam装饰的方法类型需要和@Builder方法类型一致。

    1. class GlobalBuilderParam {
    2.   labelstring = ""
    3. }
    4. @Builder function GlobalBuilder1($$ : GlobalBuilderParam) {
    5.   Text($$.label)
    6.     .width(400)
    7.     .height(50)
    8.     .backgroundColor(Color.Blue)
    9. }
    10. @Component
    11. struct Child {
    12.   @Builder componentBuilder() {
    13.     Text(`Child builder `)
    14.   }
    15.   labelstring = 'Child'
    16.   // 无参数类,指向的componentBuilder也是无参数类型
    17.   @BuilderParam aBuilder0() => void = this.componentBuilder;
    18.   // 有参数类型,指向的GlobalBuilder1也是有参数类型的方法
    19.   @BuilderParam aBuilder1($$ : GlobalBuilderParam) => void = this.componentBuilder;
    20.   build() {
    21.     Column() {
    22.       this.aBuilder0()
    23.       this.aBuilder1({label'global Builder label' } )
    24.     }
    25.   }
    26. }
    27. @Entry
    28. @Component
    29. struct Parent {
    30.   labelstring = 'Parent'
    31.   @Builder componentBuilder() {
    32.     Text(`${this.label}`)
    33.   }
    34.   build() {
    35.     Column() {
    36.       this.componentBuilder()
    37.       Child({ aBuilder0this.componentBuilderaBuilder1GlobalBuilder1 })
    38.     }
    39.   }
    40. }

    尾随闭包初始化组件

    在自定义组件中使用@BuilderParam装饰的属性时也可通过尾随闭包进行初始化。在初始化自定义组件时,组件后紧跟一个大括号“{}”形成尾随闭包场景。

    说明:

    此场景下自定义组件内有且仅有一个使用@BuilderParam装饰的属性。

    开发者可以将尾随闭包内的内容看做@Builder装饰的函数传给@BuilderParam。示例如下:

    1. // xxx.ets
    2. class CustomContainerParam {
    3. header: string = '';
    4. }
    5. @Component
    6. struct CustomContainer {
    7. @Builder componentCloser() {
    8. Text(`Custom closer `)
    9. }
    10. @Prop header: string = '';
    11. @BuilderParam closer: () => void = this.componentCloser;
    12. build() {
    13. Column() {
    14. Text(this.header)
    15.         .fontSize(30)
    16.       this.closer()
    17. }
    18. }
    19. }
    20. @Builder function specificParam(label1string, label2string) {
    21. Column() {
    22. Text(label1)
    23.       .fontSize(30)
    24. Text(label2)
    25.       .fontSize(30)
    26. }
    27. }
    28. @Entry
    29. @Component
    30. struct CustomContainerUser {
    31. @State text: string = 'header';
    32. param: CustomContainerParam = {
    33. header: this.text
    34. };
    35. build() {
    36. Column() {
    37. // 创建CustomContainer,在创建CustomContainer时,通过其后紧跟一个大括号“{}”形成尾随闭包
    38. // 作为传递给子组件CustomContainer @BuilderParam closer: () => void的参数
    39. CustomContainer(this.param) {
    40. Column() {
    41. specificParam('testA', 'testB')
    42. }.backgroundColor(Color.Yellow)
    43.         .onClick(() => {
    44.           this.text = 'changeHeader';
    45. })
    46. }
    47. }
    48. }
    49. }
  • 相关阅读:
    Bearly:基于人工智能的AI写作文章生成工具
    分类判别式模型——逻辑斯特回归曲线
    Containerd【轻量级容器管理工具】
    【Java集合类面试二十五】、有哪些线程安全的List?
    7 航空公司客户价值分析
    2023前端面试题
    springMvc57-简单的拦截器
    工程伦理--14.4 中国工程跨文化实践的伦理规范
    【StreamSets】StreamSets 运行性能检测和优化
    SSL是什么?关于SSL和TLS的常见问题
  • 原文地址:https://blog.csdn.net/OpenHarmony_dev/article/details/133309600