#HarmonyOS:@Styles装饰器:定义组件重用样式

@Styles可以定义在组件内或全局,在全局定义时需在方法名前面添加function关键字,组件内定义时则不需要添加function关键字。

组件内@Styles的优先级高于全局@Styles。

框架优先找当前组件内的@Styles,如果找不到,则会全局查找。

ts 复制代码
// 全局
@Styles function functionName() { ... }

// 在组件内
@Component
struct FancyUse {
  @Styles fancy() {
    .height(100)
  }
}
ts 复制代码
// 定义在全局的@Styles封装的样式
@Styles function globalFancy  () {
  .width(150)
  .height(100)
  .backgroundColor(Color.Pink)
}

@Entry
@Component
struct FancyUse {
  @State heightValue: number = 100
  // 定义在组件内的@Styles封装的样式
  @Styles fancy() {
    .width(200)
    .height(this.heightValue)
    .backgroundColor(Color.Yellow)
    .onClick(() => {
      this.heightValue = 200
    })
  }

  build() {
    Column({ space: 10 }) {
      // 使用全局的@Styles封装的样式
      Text('FancyA')
        .globalFancy ()
        .fontSize(30)
      // 使用组件内的@Styles封装的样式
      Text('FancyB')
        .fancy()
        .fontSize(30)
    }
  }
}
相关推荐
kyriewen113 分钟前
你点的“刷新”是假刷新?前端路由的瞒天过海术
开发语言·前端·javascript·ecmascript·html5
Timer@1 小时前
LangChain 教程 04|Agent 详解:让 AI 学会“自己干活“
javascript·人工智能·langchain
阿珊和她的猫2 小时前
TypeScript中的never类型: 深入理解never类型的使用场景和特点
javascript·typescript·状态模式
skywalk81632 小时前
Kotti Next的tinyfrontend前端模仿Kotti 首页布局还是不太好看,感觉比Kotti差一点
前端
RopenYuan4 小时前
FastAPI -API Router的应用
前端·网络·python
走粥4 小时前
clsx和twMerge解决CSS类名冲突问题
前端·css
Purgatory0015 小时前
layui select重新渲染
前端·layui
weixin199701080165 小时前
《中国供应商商品详情页前端性能优化实战》
前端·性能优化
九皇叔叔6 小时前
003-SpringSecurity-Demo 统一响应类
java·javascript·spring·springsecurity