自学鸿蒙HarmonyOS的ArkTS语言<十>@BuilderParam装饰器

作用:当子组件多处使用时,给某处的子组件添加特定功能

一、初始化

1、只能被@Builder装饰的方法初始化

2、使用所属自定义组件的@builder方法初始化

3、使用父组件的@builder方法初始化 - 把父组件的@builder传过去,参数名和子组件的@builderParam同名

java 复制代码
@Component
struct Child {
  @Builder childBuilder() {}
  @BuilderParam childBuilderParam: () => void = this.childBuilder // 必须用childBuilder初始化下,否则预览出不来

  build() {
    Column() {
      Text('我是子组件')
        .fontColor(Color.White)
      this.childBuilderParam()
    }
  }
}

@Entry
@Component
struct Index7 {

  @Builder parentBuilder() {
    Text('我是父组件定制的的 builder')

  }

  build() {
    Column() {
      Row() {
        Child({childBuilderParam: this.parentBuilder}) // 添加独特功能
      }
      .padding(10)
      .backgroundColor(Color.Brown)

      Row() {
        Child()
      }
      .padding(10)
      .backgroundColor(Color.Green)

    }
  }
}
二、this指向
java 复制代码
@Component
struct Child1 {
  label: string = '我是子组件的label'
  @Builder childBuilder() {}
  @BuilderParam childBuilderParam: () => void = this.childBuilder
  @BuilderParam childChangeThisBuilderParam: () => void = this.childBuilder

  build() {
    Column() {
      this.childBuilderParam()
      this.childChangeThisBuilderParam()
    }
  }
}

@Entry
@Component
struct Index7_1 {
  label: string = '我是父组件的label'

  @Builder parentBuilder() {
    Text(this.label)

  }

  build() {
    Column() {
      this.parentBuilder() // this指向父组件


      Child1({
        childBuilderParam: this.parentBuilder, // this.parentBuilder传入到child中指向child
        childChangeThisBuilderParam: (): void => this.parentBuilder(), // 箭头函数的this指向宿主对象,即父组件
      })

    }
  }
}
三、带参数
java 复制代码
class Tmp {
  label: string = ''
}
// 全局builder
@Builder function globalBuilder($$: Tmp) {
  Text($$.label)
}

// Child1中
...
// 有参数
@BuilderParam childHasParamsBuilderParam: ($$: Tmp) => void = globalBuilder
build() {
  Column() {
	  ...
	  this.childHasParamsBuilderParam({label: '我是一个有参数的BuilderParam'})
  }
}
// 父组件中
...
Child1({
  ...
  childHasParamsBuilderParam: globalBuilder
})
四、尾随闭包的形式
java 复制代码
@Component
struct Child2 {
  @Builder childBuilder() {}
  // 尾随闭包的形式传入时子组件内只能有一个BuilderParam
  @BuilderParam childBuilderParam: () => void = this.childBuilder

  build() {
    Column() {
      Text('我是子组件2')
      this.childBuilderParam()
    }
    .margin({top: 30})
  }
}
// 父组件中
...
Child2() {
   Column() {
     globalBuilder({label: '我是通过尾随闭包传入的'})
   }
}

注意:

尾随闭包的形式子组件内只能有一个 @BuilderParam

相关推荐
fellow997 小时前
Electron + pnpm 在鸿蒙应用里跑起来
electron·harmonyos·openharmony·deepseek·deepseekharness
熊猫钓鱼>_>8 小时前
从闲置平板到家里的“控制大脑“:鸿蒙智慧中控面板完整实战
运维·人工智能·华为·自动化·电脑·ai编程·harmonyos
500848 小时前
React Native for OpenHarmony 实战:三方库 react-native-url-polyfill 的鸿蒙化适配指南
javascript·react native·react.js·性能优化·electron·harmonyos
熊猫钓鱼>_>11 小时前
从2D平铺到3D沉浸:我用HarmonyOS 7端侧AI做了一个全程数据不出设备的空间化私密相册
前端·人工智能·3d·华为·华为云·harmonyos·鸿蒙
m0_7381858212 小时前
Flutter 鸿蒙化实战:foundation_fluttify 适配 OpenHarmony,Fluttify 桥接层
flutter·华为·harmonyos·鸿蒙
m0_7381858212 小时前
Flutter 鸿蒙化实战:http_proxy 适配 OpenHarmony,HTTP 代理
flutter·http·华为·harmonyos·鸿蒙
m0_7381858212 小时前
Flutter 鸿蒙化实战:headset_connection_event 适配 OpenHarmony,耳机插拔监听
flutter·华为·harmonyos·鸿蒙
5008413 小时前
React Native for OpenHarmony 实战:三方库 react-native-volume-control 的鸿蒙化适配指南
javascript·react native·react.js·electron·harmonyos
李游Leo1 天前
HarmonyOS 7 DualCart 平行视界适配实录 04:EasyGo × 虚拟容器:商品比价双详情与分栏比例策略【鸿蒙心迹】
华为·harmonyos
李游Leo1 天前
HarmonyOS 7 PixelBridge 原生库适配实录 06:Release 性能基线、资源释放、包体积与工程化验收【鸿蒙心迹】
华为·harmonyos