当前位置:   article > 正文

HarmonyOS学习路之方舟开发框架—学习ArkTS语言(基本语法 四)_arkts开发语言

arkts开发语言

@BuilderParam装饰器:引用@Builder函数

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

装饰器使用说明

初始化@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. @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组件在调用this.componentBuilder()时,this.label指向其所属组件,即“Parent”。@Builder componentBuilder()传给子组件@BuilderParam aBuilder0,在Child组件中调用this.aBuilder0()时,this.label指向在Child的label,即“Child”。

  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. }

使用场景

参数初始化组件

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

  1. @Builder function GlobalBuilder1($$ : {label: string }) {
  2. Text($$.label)
  3. .width(400)
  4. .height(50)
  5. .backgroundColor(Color.Blue)
  6. }
  7. @Component
  8. struct Child {
  9. label: string = 'Child'
  10. // 无参数类,指向的componentBuilder也是无参数类型
  11. @BuilderParam aBuilder0: () => void;
  12. // 有参数类型,指向的GlobalBuilder1也是有参数类型的方法
  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装饰的属性时也可通过尾随闭包进行初始化。在初始化自定义组件时,组件后紧跟一个大括号“{}”形成尾随闭包场景。

说明

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

将尾随闭包内的内容看做@Builder装饰的函数传给@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. // 创建CustomContainer,在创建CustomContainer时,通过其后紧跟一个大括号“{}”形成尾随闭包
  29. // 作为传递给子组件CustomContainer @BuilderParam closer: () => void的参数
  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. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/100925
推荐阅读
相关标签
  

闽ICP备14008679号