首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >OpenHarmony装饰指定自定义组件:@BuilderParam装饰器

OpenHarmony装饰指定自定义组件:@BuilderParam装饰器

原创
作者头像
小帅聊鸿蒙
发布2025-05-12 13:54:01
发布2025-05-12 13:54:01
1840
举报
文章被收录于专栏:鸿蒙开发笔记鸿蒙开发笔记

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

说明: 从API version 9开始,该装饰器支持在ArkTS卡片中使用。

装饰器使用说明

初始化@BuilderParam装饰的方法

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

  • 使用所属自定义组件的自定义构建函数或者全局的自定义构建函数,在本地初始化@BuilderParam。
代码语言:ts
复制
@Builder function GlobalBuilder0() {}
 
@Component
struct Child {
  @Builder doNothingBuilder() {};
 
  @BuilderParam aBuilder0: () => void = this.doNothingBuilder;
  @BuilderParam aBuilder1: () => void = GlobalBuilder0;
  build(){}
}
  • 用父组件自定义构建函数初始化子组件@BuildParam装饰的方法。
代码语言:ts
复制
@Component
struct Child {
  @Builder componentBuilder() {
    Text(`Parent builder `)
  }
 
  @BuilderParam aBuilder0: () => void = this.componentBuilder;
 
  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”。对于@BuilderParam aBuilder1,在将this.componentBuilder传给aBuilder1时,调用bind绑定了this,因此其this.label指向Parent的label。

说明:

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

代码语言:ts
复制
@Component
struct Child {
  @Builder componentBuilder() {
    Text(`Child builder `)
  }
 
  label: string = `Child`
  @BuilderParam aBuilder0: () => void = this.componentBuilder;
  @BuilderParam aBuilder1: () => void = this.componentBuilder;
 
  build() {
    Column() {
      this.aBuilder0()
      this.aBuilder1()
    }
  }
}
 
@Entry
@Component
struct Parent {
  label: string = `Parent`
 
  @Builder componentBuilder() {
    Text(`${this.label}`)
  }
 
  build() {
    Column() {
      this.componentBuilder()
      Child({ aBuilder0: this.componentBuilder, aBuilder1: this.componentBuilder })
    }
  }
}

使用场景

参数初始化组件

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

代码语言:ts
复制
class GlobalBuilderParam {
  label: string = ""
}
 
@Builder function GlobalBuilder1($$ : GlobalBuilderParam) {
  Text($$.label)
    .width(400)
    .height(50)
    .backgroundColor(Color.Blue)
}
 
@Component
struct Child {
  @Builder componentBuilder() {
    Text(`Child builder `)
  }
 
  label: string = 'Child'
  // 无参数类,指向的componentBuilder也是无参数类型
  @BuilderParam aBuilder0: () => void = this.componentBuilder;
  // 有参数类型,指向的GlobalBuilder1也是有参数类型的方法
  @BuilderParam aBuilder1: ($$ : GlobalBuilderParam) => void = this.componentBuilder;
 
  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 })
    }
  }
}
DD一下:欢迎大家关注工粽号<程序猿百晓生>,可以了解到以下知识点。
代码语言:erlang
复制
`欢迎大家关注工粽号<程序猿百晓生>,可以了解到以下知识点。`
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案) 
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......

尾随闭包初始化组件

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

说明:

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

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

代码语言:ts
复制
// xxx.ets
class CustomContainerParam {
  header: string = '';
}
@Component
struct CustomContainer {
  @Builder componentCloser() {
    Text(`Custom closer `)
  }
 
  @Prop header: string = '';
  @BuilderParam closer: () => void = this.componentCloser;
 
  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';
  param: CustomContainerParam = {
    header: this.text
  };
 
  build() {
    Column() {
      // 创建CustomContainer,在创建CustomContainer时,通过其后紧跟一个大括号“{}”形成尾随闭包
      // 作为传递给子组件CustomContainer @BuilderParam closer: () => void的参数
      CustomContainer(this.param) {
        Column() {
          specificParam('testA', 'testB')
        }.backgroundColor(Color.Yellow)
        .onClick(() => {
          this.text = 'changeHeader';
        })
      }
    }
  }
}

写在最后

如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:

  • 点赞,转发,有你们的 『点赞和评论』,才是我创造的动力;
  • 关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识;
  • 想要获取更多完整鸿蒙最新学习知识点,可关注B站:码牛课堂;

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 装饰器使用说明
    • 初始化@BuilderParam装饰的方法
  • 使用场景
    • 参数初始化组件
      • DD一下:欢迎大家关注工粽号<程序猿百晓生>,可以了解到以下知识点。
  • 尾随闭包初始化组件
  • 写在最后
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档