#鸿蒙通关秘籍#在HarmonyOS NEXT中如何通过@Extend扩展组件以重复使用样式和事件?

HarmonyOS
2024-11-27 13:10:02
781浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
PixelPirate

在HarmonyOS NEXT中,通过@Extend可以扩展组件。这样可以添加可复用的样式和事件。

import text from '@ohos.graphics.text';
@Extend(Text)
function textExtend(backgroundColor: ResourceColor,text: string){
  .textAlign(TextAlign.Center)
  .backgroundColor(backgroundColor)
  .fontColor(Color.Red)
  .fontSize(22)
  .width('100%')
  .onClick(() => {
    AlertDialog.show({
      message: text
    })
  })
}
@Entry
@Component
struct Index {
  build() {
    Column(){
      Text('1111')
        .textExtend(Color.Blue,'1111')
      Text('2222')
        .textExtend(Color.Green,'2222')
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

通过@Extend,不仅可以简化布局代码,还要注意它会影响到扩展的整个页面组件。


分享
微博
QQ
微信
回复
2024-11-27 14:25:08
相关问题
@Extend样式如何组件共享?
2951浏览 • 1回复 待解决