說明: 從 API version 9 開始,該裝飾器支持在 ArkTS 卡片中使用。
裝飾器使用說明
語法
@Extend(UIComponentName) function functionName { ... }
使用規(guī)則
? ● 和 @Styles 不同,@Extend 僅支持定義在全局,不支持在組件內(nèi)部定義。
? ● 和 @Styles 不同,@Extend 支持封裝指定的組件的私有屬性和私有事件,以及預定義相同組件的 @Extend 的方法。
// @Extend(Text)可以支持Text的私有屬性fontColor @Extend(Text) function fancy () { .fontColor(Color.Red) } // superFancyText可以調(diào)用預定義的fancy @Extend(Text) function superFancyText(size:number) { .fontSize(size) .fancy() }
? ● 和 @Styles 不同,@Extend 裝飾的方法支持參數(shù),開發(fā)者可以在調(diào)用時傳遞參數(shù),調(diào)用遵循 TS 方法傳值調(diào)用。
// xxx.ets @Extend(Text) function fancy (fontSize: number) { .fontColor(Color.Red) .fontSize(fontSize) } @Entry @Component struct FancyUse { build() { Row({ space: 10 }) { Text('Fancy') .fancy(16) Text('Fancy') .fancy(24) } } }
? ● @Extend 裝飾的方法的參數(shù)可以為 function,作為 Event 事件的句柄。
@Extend(Text) function makeMeClick(onClick: () => void) { .backgroundColor(Color.Blue) .onClick(onClick) } @Entry @Component struct FancyUse { @State label: string = 'Hello World'; onClickHandler() { this.label = 'Hello ArkUI'; } build() { Row({ space: 10 }) { Text(`${this.label}`) .makeMeClick(this.onClickHandler) } } }
? ● @Extend 的參數(shù)可以為狀態(tài)變量,當狀態(tài)變量改變時,UI 可以正常的被刷新渲染。
@Extend(Text) function fancy (fontSize: number) { .fontColor(Color.Red) .fontSize(fontSize) } @Entry @Component struct FancyUse { @State fontSizeValue: number = 20 build() { Row({ space: 10 }) { Text('Fancy') .fancy(this.fontSizeValue) .onClick(() => { this.fontSizeValue = 30 }) } } }
使用場景
以下示例聲明了 3 個 Text 組件,每個 Text 組件均設置了 fontStyle、fontWeight 和 backgroundColor 樣式。
@Entry @Component struct FancyUse { @State label: string = 'Hello World' build() { Row({ space: 10 }) { Text(`${this.label}`) .fontStyle(FontStyle.Italic) .fontWeight(100) .backgroundColor(Color.Blue) Text(`${this.label}`) .fontStyle(FontStyle.Italic) .fontWeight(200) .backgroundColor(Color.Pink) Text(`${this.label}`) .fontStyle(FontStyle.Italic) .fontWeight(300) .backgroundColor(Color.Orange) }.margin('20%') } }
@Extend 將樣式組合復用,示例如下。
@Extend(Text) function fancyText(weightValue: number, color: Color) { .fontStyle(FontStyle.Italic) .fontWeight(weightValue) .backgroundColor(color) }
通過 @Extend 組合樣式后,使得代碼更加簡潔,增強可讀性。
@Entry @Component struct FancyUse { @State label: string = 'Hello World' build() { Row({ space: 10 }) { Text(`${this.label}`) .fancyText(100, Color.Blue) Text(`${this.label}`) .fancyText(200, Color.Pink) Text(`${this.label}`) .fancyText(300, Color.Orange) }.margin('20%') } }
審核編輯 黃宇
-
API
+關(guān)注
關(guān)注
2文章
1560瀏覽量
63514 -
鴻蒙
+關(guān)注
關(guān)注
59文章
2503瀏覽量
43765 -
OpenHarmony
+關(guān)注
關(guān)注
26文章
3820瀏覽量
18125
發(fā)布評論請先 登錄
如何在KaihongOS操作系統(tǒng)上寫一個彈窗組件
KaihongOS操作系統(tǒng):Button按鈕組件介紹
蜂鳥板上Openharmony系統(tǒng)跑QT程序
think-cell:自定義think-cell(二)

鴻蒙原生頁面高性能解決方案上線OpenHarmony社區(qū) 助力打造高性能原生應用
【每天學點AI】一個例子帶你了解Python裝飾器到底在干嘛!

基于ArkTS語言的OpenHarmony APP應用開發(fā):圖片顯示器
基于ArkTS語言的OpenHarmony APP應用開發(fā):簡易計數(shù)器
基于ArkTS語言的OpenHarmony APP應用開發(fā):HelloOpenharmony
基于鴻蒙Next模擬掃圖識物的一個過程
龍芯2K0300蜂鳥板支持OpenHarmony 4.0 Release版本操作系統(tǒng)
鴻蒙ArkTS聲明式開發(fā):跨平臺支持列表【組件內(nèi)容模糊】 通用屬性

評論