OpenHarmony 定义扩展组件样式:@Extend 装饰器

说明: 从 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@componentstruct 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@componentstruct 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@componentstruct 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@componentstruct 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@componentstruct 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%') }}


quartus是什么_quartus有什么用
华为发布新一代互联网基础设施解决方案,助力ISP行业绿色数字化
高通历年芯片发展看智能手机领域变迁
稳压器功率的选择 稳压器的选配
新亮智能推出的系列VCSEL芯片产品,无疑是一抹亮色
OpenHarmony 定义扩展组件样式:@Extend 装饰器
RC正弦波振荡电路设计(二)
CEVA推出Wi-Fi IP平台 以帮助广泛终端设备实现联网功能
植物营养元素快速检测仪的产品概述
诺基亚9什么时候上市?诺基亚9配置证实:骁龙835+5.3英寸2K屏,配置强劲,性能逆天
人工智能是“我们这个时代最强大的单一力量”
Windows CE系统
DN80旋进旋涡流量计的特点与技术参数
脊叶网络架构下的布线系统说明
中国电信发布便携卫星系统招标公告,用于全面提高应急通信的保障
智能视频分析边缘计算盒子IVP07A简介
北京石景山区副区长李文化一行领导莅临赛思全球研创总部调研指导
森林防火无线监控系统的应用方案及实施方法
宏光mini EV处在一个快速爬升期,产量已经拉到了2万
以三菱FX2-32M为例,讲解PLC原理与应用