@BuilderParam裝飾器:引用@Builder函數(shù)
當(dāng)創(chuàng)建了自定義組件,并想對該組件添加特定功能時(shí),例如在自定義組件中添加一個(gè)點(diǎn)擊跳轉(zhuǎn)操作。若直接在組件內(nèi)嵌入事件方法,將會(huì)導(dǎo)致所有引入該自定義組件的地方均增加了該功能。為解決此問題,ArkUI引入了@BuilderParam裝飾器,@BuilderParam用來裝飾指向@Builder方法的變量,開發(fā)者可在初始化自定義組件時(shí)對此屬性進(jìn)行賦值,為自定義組件增加特定的功能。該裝飾器用于聲明任意UI描述的一個(gè)元素,類似slot占位符。
裝飾器使用說明
初始化@BuilderParam裝飾的方法
@BuildParam裝飾的方法只能被自定義構(gòu)建函數(shù)(@Builder裝飾的方法)初始化。
- 使用所屬自定義組件的自定義構(gòu)建函數(shù)或者全局的自定義構(gòu)建函數(shù),在本地初始化@BuilderParam。
@Builder function GlobalBuilder0() {}
@Component
struct Child {
@Builder doNothingBuilder() {};
@BuilderParam aBuilder0: () => void = this.doNothingBuilder;
@BuilderParam aBuilder1: () => void = GlobalBuilder0;
build(){}
}
- 用父組件自定義構(gòu)建函數(shù)初始化子組件@BuildParam裝飾的方法。
@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 })
}
}
}
- 需注意this指向正確。
以下示例中,Parent組件在調(diào)用this.componentBuilder()時(shí),this.label指向其所屬組件,即“Parent”。@Builder componentBuilder()傳給子組件@BuilderParam aBuilder0,在Child組件中調(diào)用this.aBuilder0()時(shí),this.label指向在Child的label,即“Child”。
@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 })
}
}
}
使用場景
參數(shù)初始化組件
@BuilderParam裝飾的方法可以是有參數(shù)和無參數(shù)的兩種形式,需與指向的@Builder方法類型匹配。@BuilderParam裝飾的方法類型需要和@Builder方法類型一致。
@Builder function GlobalBuilder1($$ : {label: string }) {
Text($$.label)
.width(400)
.height(50)
.backgroundColor(Color.Blue)
}
@Component
struct Child {
label: string = 'Child'
// 無參數(shù)類,指向的componentBuilder也是無參數(shù)類型
@BuilderParam aBuilder0: () => void;
// 有參數(shù)類型,指向的GlobalBuilder1也是有參數(shù)類型的方法
@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裝飾的屬性時(shí)也可通過尾隨閉包進(jìn)行初始化。在初始化自定義組件時(shí),組件后緊跟一個(gè)大括號“{}”形成尾隨閉包場景。
說明
此場景下自定義組件內(nèi)有且僅有一個(gè)使用@BuilderParam裝飾的屬性。文章來源:http://www.zghlxwxcb.cn/news/detail-582990.html
將尾隨閉包內(nèi)的內(nèi)容看做@Builder裝飾的函數(shù)傳給@BuilderParam。示例如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-582990.html
// 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時(shí),通過其后緊跟一個(gè)大括號“{}”形成尾隨閉包
// 作為傳遞給子組件CustomContainer @BuilderParam closer: () => void的參數(shù)
CustomContainer({ header: this.text }) {
Column() {
specificParam('testA', 'testB')
}.backgroundColor(Color.Yellow)
.onClick(() => {
this.text = 'changeHeader';
})
}
}
}
}
到了這里,關(guān)于HarmonyOS學(xué)習(xí)路之方舟開發(fā)框架—學(xué)習(xí)ArkTS語言(基本語法 四)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!