HarmonyOS NEXT学习——@BuilderParam装饰器

初步理解,相当于VUE的插槽slot


@Builder function overBuilder() {}

@Component
struct Child {
  label: string = `Child`
  @Builder customBuilder() {}
  @Builder customChangeThisBuilder() {}
  @BuilderParam customBuilderParam: () => void = this.customBuilder; // 使用自定义组件的自定义构建函数初始化@BuilderParam
  @BuilderParam customChangeThisBuilderParam: () => void = this.customChangeThisBuilder; // 使用自定义组件的自定义构建函数初始化@BuilderParam
  @BuilderParam customOverBuilderParam: () => void = overBuilder; // 使用全局自定义构建函数初始化@BuilderParam
  build() {
    Column() {
      this.customBuilderParam()
      this.customChangeThisBuilderParam()
      this.customOverBuilderParam()
    }
  }
}

@Entry
@Component
struct Parent {
  label: string = `Parent`
  button: string =`are you ok??`
  
  @Builder componentBuilder() {
    Text(`${this.label}`)
      .fontSize(50)
  }
  @Builder customChangeOverBuilderParam(){
  	Button(this.label)
  }

  build() {
    Column() {
      Child({ customBuilderParam: this.componentBuilder }) //this指向的是Parent的componentBuilder,componentBuilder里的this指向的是Child,所以label的值为“Child”
      Child({ customChangeThisBuilderParam: ():void=>{this.componentBuilder()}}) //通过():void=>{this.componentBuilder()}的形式传给子组件,因为箭头函数的this指向的是宿主对象,所以label的值为“Parent”
      Child({ customBuilderParam: this.componentBuilder,customChangeThisBuilderParam: ():void=>{this.componentBuilder()},customOverBuilderParam:():void=>{this.customChangeOverBuilderParam()}}) //正确组合写法,上面两条分别调Child为了区分
    }
  }

在这里插入图片描述

相关推荐

  1. ArkTS学习笔记_封装复用之@builderParam装饰

    2024-07-18 19:06:03       17 阅读
  2. Python学习(1):装饰

    2024-07-18 19:06:03       42 阅读
  3. ArkTs 语法学习 ---- 组件相关装饰

    2024-07-18 19:06:03       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 19:06:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 19:06:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 19:06:03       58 阅读
  4. Python语言-面向对象

    2024-07-18 19:06:03       69 阅读

热门阅读

  1. 探索HiFi智能编解码器的奇妙世界

    2024-07-18 19:06:03       19 阅读
  2. 大话设计模式

    2024-07-18 19:06:03       18 阅读
  3. QT老版本下载指南

    2024-07-18 19:06:03       22 阅读
  4. react native 截图并保存到相册

    2024-07-18 19:06:03       21 阅读
  5. MySQL入门学习-深入索引.函数和表达式索引

    2024-07-18 19:06:03       25 阅读
  6. 超撒加密2.0

    2024-07-18 19:06:03       27 阅读
  7. P1009 [NOIP1998 普及组] 阶乘之和

    2024-07-18 19:06:03       22 阅读