自学鸿蒙HarmonyOS的ArkTS语言<十>@BuilderParam装饰器
创始人
2024-12-28 02:09:55
0

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

一、初始化

1、只能被@Builder装饰的方法初始化
2、使用所属自定义组件的@builder方法初始化
3、使用父组件的@builder方法初始化 - 把父组件的@builder传过去,参数名和子组件的@builderParam同名

@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指向
@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指向宿主对象,即父组件       })      }   } } 

在这里插入图片描述

三、带参数
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 }) 
四、尾随闭包的形式
@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

相关内容

热门资讯

一秒答解!aapoker辅助工... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
科普!边锋老友圈怎么开辅助,h... 科普!边锋老友圈怎么开辅助,hhpoker是真的还是假的,高科技教程(有挂细节)-哔哩哔哩;边锋老友...
详细一下!小南四川长牌辅助,w... 详细一下!小南四川长牌辅助,wepoker辅助脚本,都是真的是有挂(哔哩哔哩)1、每一步都需要思考,...
推荐几款新版!拱趴大菠萝机器人... 推荐几款新版!拱趴大菠萝机器人,we-poker辅助软件教程,其实真的有挂(真的有挂)1、免费辅助多...
研究成果!wepoker破解版... 研究成果!wepoker破解版内购,wpk透视插件,普及教程(有挂分析)-哔哩哔哩;亲真的是有正版授...
科普一下!福建大玩家十三水辅助... 科普一下!福建大玩家十三水辅助,陕麻圈脚本辅助,一贯存在有挂(哔哩哔哩)1、上手简单,内置详细流程视...
必知教程!wpk辅助器是真的吗... 必知教程!wpk辅助器是真的吗,hhpoker开挂教程,切实真的有挂(有挂猫腻)破解侠是真的助透视。...
科技介绍!拱趴大菠萝技巧,红龙... 科技介绍!拱趴大菠萝技巧,红龙poker辅助平台,系统教程(发现有挂)-哔哩哔哩;拱趴大菠萝技巧AI...
解谜一下!乐平包王讨赏辅助,逸... 解谜一下!乐平包王讨赏辅助,逸趣鄱阳翻精辅助,原来是真的有挂(哔哩哔哩)1.乐平包王讨赏辅助 选牌创...
信息共享!德州透视插件,wep... 信息共享!德州透视插件,wepoker私人局辅助器怎么用,果然是真的有挂(真实有挂)1、wepoke...