說明: 從 API version 9 開始,該裝飾器支持在 ArkTS 卡片中使用。
裝飾器使用說明
語法
@Extend(UIComponentName) function functionName { ... }
使用規則
? ● 和 @Styles 不同,@Extend 僅支持定義在全局,不支持在組件內部定義。
? ● 和 @Styles 不同,@Extend 支持封裝指定的組件的私有屬性和私有事件,以及預定義相同組件的 @Extend 的方法。
// @Extend(Text)可以支持Text的私有屬性fontColor
@Extend(Text) function fancy () {
.fontColor(Color.Red)
}
// superFancyText可以調用預定義的fancy
@Extend(Text) function superFancyText(size:number) {
.fontSize(size)
.fancy()
}
? ● 和 @Styles 不同,@Extend 裝飾的方法支持參數,開發者可以在調用時傳遞參數,調用遵循 TS 方法傳值調用。
// 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 裝飾的方法的參數可以為 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 的參數可以為狀態變量,當狀態變量改變時,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
+關注
關注
2文章
2372瀏覽量
66788 -
鴻蒙
+關注
關注
60文章
2963瀏覽量
45905 -
OpenHarmony
+關注
關注
33文章
3952瀏覽量
21103
發布評論請先 登錄
相關推薦
熱點推薦
無法定位和自定義組件怎么解決?
安裝EZ-PD? SDK3.6 后, PSoC? Creator 抱怨未知組件。 其中一個例子是
然后,我打開組件更新工具,發現編譯器找不到特定版本的組件,見下圖。
請提供幫助,以
發表于 11-11 06:04
飛書富文本組件庫RichTextVista開源
近日,飛書正式將其自研的富文本組件庫 RichTextVista(簡稱“RTV”)開源,并上線OpenHarmony 三方庫中心倉。該組件以領先的性能、流暢的渲染體驗與高度的開放性,為鴻蒙生態提供了更高效的富文本解決方案。
關于生命周期中的aboutToAppear和onPageShow的理解和應用
過程、應用進入前臺等場景,僅@Entry裝飾的自定義組件作為頁面時生效。
從兩者相同的角度來說,其都是在自定義組件顯示后,主動去觸發的生命周
發表于 06-30 17:32
HarmonyOS基礎組件:Button三種類型的使用
中的Button相較于Android原生來說,功能比較豐富,擴展性高,減少了開發者的代碼數量,簡化了使用方式。不僅可以自定義圓角還支持三種樣式。 常用屬性 名稱 參數類型 描述 type
貢獻 OpenHarmony 庫關鍵配置
、組件等
└─oh-package.json5// HAR的描述文件,定義HAR的基本信息、依賴項等
library/build-profile.json5 建議開啟代碼混淆
{
\"
發表于 05-28 13:46
層疊布局 (Stack):Stack組件為容器組件,容器內可包含各種子元素
自己的樣式定義以及排列。
let MTop:Record = { 'top': 50 }Column(){Stack({ }) { Column(){}.width
發表于 04-30 07:51
KaihongOS操作系統:頁面的生命周期介紹
頁面的生命周期
在KaihongOS中,學習頁面的生命周期前需要先了解自定義組件。
1. 自定義組件(Component)
自定義
發表于 04-25 08:18
KaihongOS操作系統:Button按鈕組件介紹
Button
按鈕組件,可快速創建不同樣式的按鈕。
常用接口
Button
Button(options: ButtonOptions)
創建可以包含單個子組件的按鈕。
參數:
參數名類型必填
發表于 04-25 07:09
OpenHarmony 定義擴展組件樣式:@Extend 裝飾器
評論