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

打印 上一主题 下一主题

主题 534|帖子 534|积分 1602

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

1、只能被@Builder装饰的方法初始化
2、使用所属自定义组件的@builder方法初始化
3、使用父组件的@builder方法初始化 - 把父组件的@builder传过去,参数名和子组件的@builderParam同名
  1. @Component
  2. struct Child {
  3.   @Builder childBuilder() {}
  4.   @BuilderParam childBuilderParam: () => void = this.childBuilder // 必须用childBuilder初始化下,否则预览出不来
  5.   build() {
  6.     Column() {
  7.       Text('我是子组件')
  8.         .fontColor(Color.White)
  9.       this.childBuilderParam()
  10.     }
  11.   }
  12. }
  13. @Entry
  14. @Component
  15. struct Index7 {
  16.   @Builder parentBuilder() {
  17.     Text('我是父组件定制的的 builder')
  18.   }
  19.   build() {
  20.     Column() {
  21.       Row() {
  22.         Child({childBuilderParam: this.parentBuilder}) // 添加独特功能
  23.       }
  24.       .padding(10)
  25.       .backgroundColor(Color.Brown)
  26.       Row() {
  27.         Child()
  28.       }
  29.       .padding(10)
  30.       .backgroundColor(Color.Green)
  31.     }
  32.   }
  33. }
复制代码
二、this指向

  1. @Component
  2. struct Child1 {
  3.   label: string = '我是子组件的label'
  4.   @Builder childBuilder() {}
  5.   @BuilderParam childBuilderParam: () => void = this.childBuilder
  6.   @BuilderParam childChangeThisBuilderParam: () => void = this.childBuilder
  7.   build() {
  8.     Column() {
  9.       this.childBuilderParam()
  10.       this.childChangeThisBuilderParam()
  11.     }
  12.   }
  13. }
  14. @Entry
  15. @Component
  16. struct Index7_1 {
  17.   label: string = '我是父组件的label'
  18.   @Builder parentBuilder() {
  19.     Text(this.label)
  20.   }
  21.   build() {
  22.     Column() {
  23.       this.parentBuilder() // this指向父组件
  24.       Child1({
  25.         childBuilderParam: this.parentBuilder, // this.parentBuilder传入到child中指向child
  26.         childChangeThisBuilderParam: (): void => this.parentBuilder(), // 箭头函数的this指向宿主对象,即父组件
  27.       })
  28.     }
  29.   }
  30. }
复制代码

三、带参数

  1. class Tmp {
  2.   label: string = ''
  3. }
  4. // 全局builder
  5. @Builder function globalBuilder($$: Tmp) {
  6.   Text($$.label)
  7. }
  8. // Child1中
  9. ...
  10. // 有参数
  11. @BuilderParam childHasParamsBuilderParam: ($$: Tmp) => void = globalBuilder
  12. build() {
  13.   Column() {
  14.           ...
  15.           this.childHasParamsBuilderParam({label: '我是一个有参数的BuilderParam'})
  16.   }
  17. }
  18. // 父组件中
  19. ...
  20. Child1({
  21.   ...
  22.   childHasParamsBuilderParam: globalBuilder
  23. })
复制代码
四、尾随闭包的形式

  1. @Component
  2. struct Child2 {
  3.   @Builder childBuilder() {}
  4.   // 尾随闭包的形式传入时子组件内只能有一个BuilderParam
  5.   @BuilderParam childBuilderParam: () => void = this.childBuilder
  6.   build() {
  7.     Column() {
  8.       Text('我是子组件2')
  9.       this.childBuilderParam()
  10.     }
  11.     .margin({top: 30})
  12.   }
  13. }
  14. // 父组件中
  15. ...
  16. Child2() {
  17.    Column() {
  18.      globalBuilder({label: '我是通过尾随闭包传入的'})
  19.    }
  20. }
复制代码

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

温锦文欧普厨电及净水器总代理

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表