样式

@Style

1抽取页面中重复样式代码,只能提取布局样式,不支持字体或文本样式 2可以写在struct组件内、组件外(function),仅在同一个文件生效 3不支持传参
抽取页面中重复样式代码,只能提取布局样式,不支持字体或文本样式 可以写在struct组件内、组件外(function),仅在同一个文件生效 不支持传参

 1@Entry  2@Component  3struct Page02_TaskList {  4 // 组件内部提取公共样式  5 @Styles  6 titleText() {  7 .width(100)  8 .height(100)  9 .backgroundColor(Color.Brown) 10 .borderRadius(10) 11 } 12 13 build() { 14 Column() { 15 // 主体内容 16 Column() 17 .titleText() 18 } 19 .height('100%') 20 .width('100%') 21 .padding(10) 22 .backgroundColor('#f1f3f5') 23 } 24}
@Entry @Component struct Page02_TaskList { // 组件内部提取公共样式 @Styles titleText() { .width(100) .height(100) .backgroundColor(Color.Brown) .borderRadius(10) } build() { Column() { // 主体内容 Column() .titleText() } .height('100%') .width('100%') .padding(10) .backgroundColor('#f1f3f5') } }

组件外

增加function关键字

 1@Styles  2function titleText2() {  3 .width(100)  4 .height(100)  5 .backgroundColor(Color.Orange)  6 .borderRadius(10)  7}  8  9@Entry 10@Component 11struct Page02_TaskList { 12 // 组件内部提取公共样式 13 @Styles 14 titleText() { 15 .width(100) 16 .height(100) 17 .backgroundColor(Color.Brown) 18 .borderRadius(10) 19 } 20 21 build() { 22 Column() { 23 // 主体内容 24 Column() 25 .titleText() 26 27 Column() 28 .titleText2() 29 } 30 .height('100%') 31 .width('100%') 32 .padding(10) 33 .backgroundColor('#f1f3f5') 34 } 35}
@Styles function titleText2() { .width(100) .height(100) .backgroundColor(Color.Orange) .borderRadius(10) } @Entry @Component struct Page02_TaskList { // 组件内部提取公共样式 @Styles titleText() { .width(100) .height(100) .backgroundColor(Color.Brown) .borderRadius(10) } build() { Column() { // 主体内容 Column() .titleText() Column() .titleText2() } .height('100%') .width('100%') .padding(10) .backgroundColor('#f1f3f5') } }

@Extend

1扩展鸿蒙原生组件样式或事件,重定义原生组件样式,类似@Styles 2必须指定组件 3支持传参
扩展鸿蒙原生组件样式或事件,重定义原生组件样式,类似@Styles 必须指定组件 支持传参

 1@Extend(Text)  2function textStyle() {  3 .fontSize(16)  4 .fontColor($r('app.color.add_title_color'))  5}  6  7@Entry  8@Component  9struct Page02_TaskList { 10 // 组件内部提取公共样式 11 @Styles 12 titleText() { 13 .width(100) 14 .height(100) 15 .backgroundColor(Color.Brown) 16 .borderRadius(10) 17 } 18 19 build() { 20 Column() { 21 // 主体内容 22 Column() { 23 Text('内容') 24 .textStyle() 25 } 26 } 27 .height('100%') 28 .width('100%') 29 .padding(10) 30 .backgroundColor('#f1f3f5') 31 } 32}
@Extend(Text) function textStyle() { .fontSize(16) .fontColor($r('app.color.add_title_color')) } @Entry @Component struct Page02_TaskList { // 组件内部提取公共样式 @Styles titleText() { .width(100) .height(100) .backgroundColor(Color.Brown) .borderRadius(10) } build() { Column() { // 主体内容 Column() { Text('内容') .textStyle() } } .height('100%') .width('100%') .padding(10) .backgroundColor('#f1f3f5') } }

Powered By 可尔物语

浙ICP备11005866号-12