W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
當開發(fā)者創(chuàng)建了自定義組件,并想對該組件添加特定功能時,例如在自定義組件中添加一個點擊跳轉操作。若直接在組件內嵌入事件方法,將會導致所有引入該自定義組件的地方均增加了該功能。為解決此問題,ArkUI引入了@BuilderParam裝飾器,@BuilderParam用來裝飾指向@Builder方法的變量,開發(fā)者可在初始化自定義組件時對此屬性進行賦值,為自定義組件增加特定的功能。該裝飾器用于聲明任意UI描述的一個元素,類似slot占位符。
從API version 9開始,該裝飾器支持在ArkTS卡片中使用。
@BuilderParam裝飾的方法只能被自定義構建函數(@Builder裝飾的方法)初始化。
- @Builder function GlobalBuilder0() {}
- @Component
- struct Child {
- @Builder doNothingBuilder() {};
- @BuilderParam aBuilder0: () => void = this.doNothingBuilder;
- @BuilderParam aBuilder1: () => void = GlobalBuilder0;
- build(){}
- }
- @Component
- struct Child {
- @BuilderParam aBuilder0: () => void;
- build() {
- Column() {
- this.aBuilder0()
- }
- }
- }
- @Entry
- @Component
- struct Parent {
- @Builder componentBuilder() {
- Text(`Parent builder `)
- }
- build() {
- Column() {
- Child({ aBuilder0: this.componentBuilder })
- }
- }
- }
開發(fā)者謹慎使用bind改變函數調用的上下文,可能會使this指向混亂。
- @Component
- struct Child {
- label: string = `Child`
- @BuilderParam aBuilder0: () => void;
- build() {
- Column() {
- this.aBuilder0()
- }
- }
- }
- @Entry
- @Component
- struct Parent {
- label: string = `Parent`
- @Builder componentBuilder() {
- Text(`${this.label}`)
- }
- build() {
- Column() {
- this.componentBuilder()
- Child({ aBuilder0: this.componentBuilder })
- }
- }
- }
@BuilderParam裝飾的方法可以是有參數和無參數的兩種形式,需與指向的@Builder方法類型匹配。@BuilderParam裝飾的方法類型需要和@Builder方法類型一致。
- @Builder function GlobalBuilder1($$ : {label: string }) {
- Text($$.label)
- .width(400)
- .height(50)
- .backgroundColor(Color.Green)
- }
- @Component
- struct Child {
- label: string = 'Child'
- // 無參數類,指向的componentBuilder也是無參數類型
- @BuilderParam aBuilder0: () => void;
- // 有參數類型,指向的GlobalBuilder1也是有參數類型的方法
- @BuilderParam aBuilder1: ($$ : { label : string}) => void;
- 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 })
- }
- }
- }
在自定義組件中使用@BuilderParam裝飾的屬性時也可通過尾隨閉包進行初始化。在初始化自定義組件時,組件后緊跟一個大括號“{}”形成尾隨閉包場景。
此場景下自定義組件內有且僅有一個使用@BuilderParam裝飾的屬性。
開發(fā)者可以將尾隨閉包內的內容看做@Builder裝飾的函數傳給@BuilderParam。示例如下:
- // xxx.ets
- @Component
- struct CustomContainer {
- @Prop header: string;
- @BuilderParam closer: () => void
- 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';
- build() {
- Column() {
- // 創(chuàng)建CustomContainer,在創(chuàng)建CustomContainer時,通過其后緊跟一個大括號“{}”形成尾隨閉包
- // 作為傳遞給子組件CustomContainer @BuilderParam closer: () => void的參數
- CustomContainer({ header: this.text }) {
- Column() {
- specificParam('testA', 'testB')
- }.backgroundColor(Color.Yellow)
- .onClick(() => {
- this.text = 'changeHeader';
- })
- }
- }
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯系方式:
更多建議: