HarmonyOS鸿蒙Next中如果增加一个style方法会优雅嘛

HarmonyOS鸿蒙Next中如果增加一个style方法会优雅嘛

Button() {
  Text('button')
}
.width(50)
.height(50)
.width('100%')
.border({ color: '#CCC', width: 2 })

// 如果上面写法换成下面写法会如何
Button() {
  Text('button')
}
.style({
  height: 50,
  height: 50,
  borderWidth: 1,
  borderColor: '#CCC'
})
8 回复

1. 使用@Styles装饰器(推荐)

// 全局定义样式
[@Styles](/user/Styles)
function buttonStyle() {
  .width('100%')
  .height(50)
  .border({ color: '#CCC', width: 2 })
}

// 组件内使用
@Entry
@Component
struct MyComponent {
  build() {
    Column() {
      Button() {
        Text('button')
      }
      .buttonStyle()  // 应用样式
    }
  }
}

2. 使用@Extend装饰器(支持参数)

// 全局定义扩展样式(支持参数)
[@Extend](/user/Extend)(Button)
function customButtonStyle(
  width: string | number = '100%',
  height: number = 50,
  borderColor: Color = Color.Gray,
  borderWidth: number = 2
) {
  .width(width)
  .height(height)
  .border({ color: borderColor, width: borderWidth })
}

// 组件内使用
@Entry
@Component
struct MyComponent {
  build() {
    Column() {
      Button() {
        Text('button')
      }
      .customButtonStyle()  // 使用默认参数
      
      Button() {
        Text('button2')
      }
      .customButtonStyle('80%', 60, Color.Red, 1)  // 自定义参数
    }
  }
}

3. 使用AttributeModifier(最灵活)

// 定义属性修改器
class ButtonStyleModifier implements AttributeModifier<ButtonAttribute> {
  private widthValue: string | number
  private heightValue: number
  private borderColorValue: Color
  private borderWidthValue: number

  constructor(style: {
    width?: string | number,
    height?: number,
    borderColor?: Color,
    borderWidth?: number
  }) {
    this.widthValue = style.width || '100%'
    this.heightValue = style.height || 50
    this.borderColorValue = style.borderColor || Color.Gray
    this.borderWidthValue = style.borderWidth || 2
  }

  applyNormalAttribute(instance: ButtonAttribute): void {
    instance
      .width(this.widthValue)
      .height(this.heightValue)
      .border({ 
        color: this.borderColorValue, 
        width: this.borderWidthValue 
      })
  }
}

// 组件内使用
@Entry
@Component
struct MyComponent {
  @State modifier: ButtonStyleModifier = new ButtonStyleModifier({
    width: '100%',
    height: 50,
    borderColor: Color.Gray,
    borderWidth: 2
  })

  build() {
    Column() {
      Button() {
        Text('button')
      }
      .attributeModifier(this.modifier)
      
      Button('动态修改样式')
        .onClick(() => {
          // 动态更新样式
          this.modifier = new ButtonStyleModifier({
            width: '80%',
            height: 60,
            borderColor: Color.Blue,
            borderWidth: 3
          })
        })
    }
  }
}

4. 组合使用@Styles和状态变量

@Entry
@Component
struct MyComponent {
  @State buttonHeight: number = 50
  @State borderWidth: number = 2
  @State borderColor: Color = Color.Gray

  [@Styles](/user/Styles)
  get dynamicButtonStyle() {
    .width('100%')
    .height(this.buttonHeight)
    .border({ color: this.borderColor, width: this.borderWidth })
  }

  build() {
    Column() {
      Button() {
        Text('button')
      }
      .dynamicButtonStyle()
      
      Button('改变样式')
        .onClick(() => {
          this.buttonHeight = 60
          this.borderWidth = 3
          this.borderColor = Color.Red
        })
    }
  }
}

对比总结

方法 优点 缺点 适用场景
@Styles 简单易用,编译期处理 不支持参数,不支持跨文件导出 简单样式复用
@Extend 支持参数,比@Styles灵活 不支持跨文件导出 需要参数化的样式
AttributeModifier 最灵活,支持业务逻辑,支持跨文件 相对复杂,需要实现接口 复杂动态样式,需要跨文件复用

推荐使用顺序

  1. 简单场景:优先使用@Styles
  2. 需要参数:使用@Extend
  3. 复杂需求:使用AttributeModifier

更多关于HarmonyOS鸿蒙Next中如果增加一个style方法会优雅嘛的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


描述:这种方式确实优雅,我也很赞同,熟悉了前端,自然会想到这一点,每种语法设计都有它的利弊和权衡,我们只需掌握语法规则,按规则办事即可

扩展:在arkts语法中,有自己的特点,例如样式有好几种设计:

  1. @sytle:公共样式进行复用的装饰器,注意“公共样式”的理解,意思是有的组件没有的属性是无法通过该装饰器服用的,参考文档:

[@Styles装饰器:定义组件重用样式-学习UI范式基本语法-UI开发 (ArkTS声明式开发范式)-ArkUI(方舟UI框架)-应用框架 - 华为HarmonyOS开发者](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-style)

  1. @Extend:封装指定组件的私有属性、私有事件和自身定义的全局方法,还有很多用法,具体参考指南:

[@Extend装饰器:定义扩展组件样式-学习UI范式基本语法-UI开发 (ArkTS声明式开发范式)-ArkUI(方舟UI框架)-应用框架 - 华为HarmonyOS开发者](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-extend)

  1. AttributeModify:属性更新器,全局样式,你可以理解为全局css样式,具体参考指南:

属性更新器 (AttributeUpdater)-Modifier机制-使用自定义能力-UI开发 (ArkTS声明式开发范式)-ArkUI(方舟UI框架)-应用框架 - 华为HarmonyOS开发者

如果只是单个页面重复使用某样式可以使用style的方式去设置样式,可以很好的减少代码冗余。

但是也需要具体情况具体分析,要根据自己的实际情况去判断如何更好的让代码量更少、工作效率更高。

如果是多个页面甚至整个工程都大量重复使用某样式,那么更建议使用一楼提到的AttributeModifier方式,而且能够动态变化样式。

也可以参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-arkui-attributeupdater

style如果不涉及传递参数的情况,重复的ui属性,可以采用style这样写会让你减少冗余的代码,而如果涉及传递参数就要用到Extend,可以针对某一类型的组件进行ui属性复用或者扩展,以上都是在当前页面完成的代码,如果按照整个项目来说可以封装大部分雷同的样式到AttributeModifier里面,最后进行导出使用,可以减少很多冗余的代码

感觉可以不支持动态修改,动态修改可以使用 连缀,岂不是很优化,

具体还是根据业务架构来的,

在HarmonyOS Next中增加style方法符合其声明式UI设计理念,通过组合式API统一管理组件样式。该方法可封装通用样式属性,支持动态响应状态变化,提升代码复用性和可维护性。采用ArkTS语法实现,与现有UI组件体系无缝集成,保持类型安全和开发效率。

在HarmonyOS Next中,增加一个style方法确实能让代码更优雅。目前的链式调用虽然直观,但当组件样式属性较多时,代码会变得冗长。使用style方法可以将样式属性集中在一个对象中,提升代码的可读性和维护性。

例如,在示例代码中,原本需要多次调用.width().height()等方法,而使用style方法后,所有样式属性一目了然,减少了重复代码。同时,这种方式也更符合现代前端框架(如React、Vue)的样式编写习惯,对开发者更友好。

不过,需要注意样式属性的命名规范(如borderWidth代替borderwidth属性),以确保一致性和易用性。总体而言,style方法是一种更简洁、高效的样式管理方式。

回到顶部