美文网首页鸿蒙
@BuilderParam装饰器:引用@Builder函数

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

作者: wendy__xu | 来源:发表于2024-03-07 18:01 被阅读0次

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

装饰器使用说明

初始化@BuilderParam装饰的方法

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

  • 使用所属自定义组件的自定义构建函数或者全局的自定义构建函数,在本地初始化@BuilderParam。
@Builder function overBuilder() {}

@Component
struct Child {
  @Builder doNothingBuilder() {};

  // 使用自定义组件的自定义构建函数初始化@BuilderParam
  @BuilderParam customBuilderParam: () => void = this.doNothingBuilder;
  // 使用全局自定义构建函数初始化@BuilderParam
  @BuilderParam customOverBuilderParam: () => void = overBuilder;
  build(){}
}
  • 用父组件自定义构建函数初始化子组件@BuilderParam装饰的方法。
@Component
struct Child {
  // 使用父组件@Builder装饰的方法初始化子组件@BuilderParam
  @BuilderParam customBuilderParam: () => void;

  build() {
    Column() {
      this.customBuilderParam()
    }
  }
}

@Entry
@Component
struct Parent {
  @Builder componentBuilder() {
    Text(`Parent builder `)
  }

  build() {
    Column() {
      Child({ customBuilderParam: this.componentBuilder })
    }
  }
}

使用场景

参数初始化组件

@Builder function overBuilder($$ : {label: string }) {
  Text($$.label)
    .width(400)
    .height(50)
    .backgroundColor(Color.Green)
}

@Component
struct Child {
  label: string = 'Child'
  // 无参数类型,指向的componentBuilder也是无参数类型
  @BuilderParam customBuilderParam: () => void;
  // 有参数类型,指向的GlobalBuilder1也是有参数类型的方法
  @BuilderParam customOverBuilderParam: ($$ : { label : string}) => void;

  build() {
    Column() {
      this.customBuilderParam()
      this.customOverBuilderParam({label: 'global Builder label' } )
    }
  }
}

@Entry
@Component
struct Parent {
  label: string = 'Parent'

  @Builder componentBuilder() {
    Text(`${this.label}`)
  }

  build() {
    Column() {
      this.componentBuilder()
      Child({ customBuilderParam: this.componentBuilder, customOverBuilderParam: overBuilder })
    }
  }
}

尾随闭包初始化组件

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

此场景下自定义组件内有且仅有一个使用@BuilderParam装饰的属性。
开发者可以将尾随闭包内的内容看做@Builder装饰的函数传给@BuilderParam。示例如下:

// xxx.ets
@Component
struct CustomContainer {
  @Prop header: string;
  // 使用父组件的尾随闭包{}(@Builder装饰的方法)初始化子组件@BuilderParam
  @BuilderParam closer: () => void

  build() {
    Column() {
      Text(this.header)
        .fontSize(30)
      this.closer()
    }
  }
}

@Builder function specificParam(label1: string, label2: string) {
  Column() {
    Text(label1)
      .fontSize(30)
    Text(label2)
      .fontSize(30)
  }
}

@Entry
@Component
struct CustomContainerUser {
  @State text: string = 'header';

  build() {
    Column() {
      // 创建CustomContainer,在创建CustomContainer时,通过其后紧跟一个大括号“{}”形成尾随闭包
      // 作为传递给子组件CustomContainer @BuilderParam closer: () => void的参数
      CustomContainer({ header: this.text }) {
        Column() {
          specificParam('testA', 'testB')
        }.backgroundColor(Color.Yellow)
        .onClick(() => {
          this.text = 'changeHeader';
        })
      }
    }
  }
}

相关文章

  • 浅析装饰器的那些事儿

    一、装饰器的简单定义 外层函数返回里层函数的引用,里层函数引用外层函数的变量。 二、装饰器的作用 通俗来讲装饰器的...

  • 函数装饰器执行顺序

    当一个函数有多个装饰器时,会优先执行离函数最近的装饰器,由于装饰器返回的是函数的函数名引用,并非真正调用函数。

  • python装饰器理解

    阅读顺序: 函数引用 函数闭包 装饰器(最好先阅读上面的两个) 1.函数引用 2.函数闭包 3.装饰器 背景:一般...

  • 装饰器

    1.装饰器的概念 装饰器是一个闭包:内层函数引用外层函数的变量(参数也算变量),然后返回内层函数,就是闭包。装饰器...

  • 装饰器

    [TOC] 函数 函数定义 函数调用 高阶函数 嵌套函数 装饰器 装饰器=高阶函数+嵌套函数 基础装饰器 假设有一...

  • 031 Python语法之装饰器

    装饰器 装饰器格式 装饰器 本质是函数 功能是用于装饰其他函数,为其他函数附加其它功能 装饰器的原则 不能修改被装...

  • python装饰器2018-11-19

    装饰器也是个函数 装饰器有参数,参数是函数 装饰器有内嵌函数 装饰器有返回值,返回值是内嵌函数 装饰器使用时直接挂...

  • python 装饰器decorator

    闭包函数:函数内部定义的函数,引用了外部变量但非全局变量。 装饰器 decorator 实质: 是一个函数 参数:...

  • Python day30_闭包与装饰器

    闭包 装饰器 装饰器结论: 一个装饰器一个函数了解 二个装饰器装饰一个函数图解大法 二个装饰器装饰器一个函数内存图...

  • python——装饰器详解

    一、装饰器概念 1、装饰器 装饰器:一种返回值也是一个函数的函数,即装饰器。 2、装饰器目的 装饰器的目的:装饰器...

网友评论

    本文标题:@BuilderParam装饰器:引用@Builder函数

    本文链接:https://www.haomeiwen.com/subject/hmmjzdtx.html