当前位置: 首页 > article >正文

HarmonyOS(七)——@BuilderParam装饰器

前言:

前面我们认识了@Builder装饰器:自定义构建函数,今天我们继续认识下一个装饰器——@BuilderParam装饰器。

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

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

@BuilderParam装饰器的使用说明

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

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

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

  @BuilderParam aBuilder0: () => void = this.doNothingBuilder;
  @BuilderParam aBuilder1: () => void = GlobalBuilder0;
  build(){}
}
  • 用父组件自定义构建函数初始化子组件@BuildParam装饰的方法。
@Component
struct Child {
  @BuilderParam aBuilder0: () => void;

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

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

  build() {
    Column() {
      Child({ aBuilder0: this.componentBuilder })
    }
  }
}
  • 需注意this指向正确。
    以下示例中,Parent组件在调用this.componentBuilder()时,this.label指向其所属组件,即“Parent”。@Builder componentBuilder()传给子组件@BuilderParam aBuilder0,在Child组件中调用this.aBuilder0()时,this.label指向在Child的label,即“Child”。
@Component
struct Child {
  label: string = `Child`
  @BuilderParam aBuilder0: () => void;

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

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

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

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

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

@BuilderParam装饰器的使用场景

1:参数初始化组件

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

举个列子:

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

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

  build() {
    Column() {
      this.aBuilder0()
      this.aBuilder1({label: 'global Builder label' } )
    }
  }
}

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

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

  build() {
    Column() {
      this.componentBuilder()
      Child({ aBuilder0: this.componentBuilder, aBuilder1: GlobalBuilder1 })
    }
  }
}

2:参数初始化组件

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

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

// xxx.ets
@Component
struct CustomContainer {
  @Prop header: string;
  @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';
        })
      }
    }
  }
}

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

总结:

  1. @BuilderParam用来装饰指向@Builder方法的变量,开发者可在初始化自定义组件时对此属性进行赋值,为自定义组件增加特定的功能。该装饰器用于声明任意UI描述的一个元素,类似slot占位符。
  2. @BuildParam装饰的方法只能被自定义构建函数(@Builder装饰的方法)初始化。
  3. 在参数初始化组件时,@BuilderParam装饰的方法可以是有参数和无参数的两种形式,需与指向的@Builder方法类型匹配。@BuilderParam装饰的方法类型需要和@Builder方法类型一致。
  4. 在尾随闭包初始化组件时,组件后紧跟一个大括号“{}”形成尾随闭包场景。开发者可以将尾随闭包内的内容看做@Builder装饰的函数传给@BuilderParam。但是注意,此场景下自定义组件内有且仅有一个使用@BuilderParam装饰的属性。

http://www.kler.cn/news/150139.html

相关文章:

  • Flink-时间窗口
  • 解决electron-builder打包不成功只能输出tgz文件的问题
  • 函数的极值与最值
  • 【C 语言经典100例】C 练习实例1
  • NMap扫描进阶
  • [黑马程序员SpringBoot2]——开发实用篇3
  • 手机一键“触达”!VR全景助力政务服务大厅数字升级
  • Linux常用命令——rm 命令
  • c语言编程题经典100例——(36~40例)
  • Es6笔记之箭头函数与解构赋值
  • Day60.算法训练
  • 基于C#实现Prim算法
  • 不同路径 II(力扣LeetCode)动态规划
  • 荒野大镖客提示找不到emp.dll文件的5个修复方法-快速修复dll教程
  • ZYNQ_project:lcd_pic_400x400
  • springboot 返回problem+json
  • 【云备份】第三方库的认识与使用
  • go模版引擎的使用~~
  • 【c语言】二维数组的对角线对称交换
  • LeetCode 60. 排列序列【数学,逆康托展开】困难
  • ⑤【Sorted Set】Redis常用数据类型: ZSet [使用手册]
  • WordPress更改文章分类插件
  • CH01_适应设计模式
  • 网络安全如何自学?
  • 深圳市东星制冷机电受邀莅临2024国际生物发酵展,济南与您相约
  • Spring的依赖注入,依赖注入的基本原则,依赖注入的优势
  • 【Vulnhub 靶场】【Coffee Addicts: 1】【简单-中等】【20210520】
  • 01_原理-事件循环
  • docker (简介、dcoker详细安装步骤、容器常用命令)一站打包- day01
  • [密码学]DES