鸿蒙开发中的常见关键字简单总结

如果总结的有问题,欢迎大家指出来!

在鸿蒙应用开发中(特别是使用 ArkUI 框架和 ArkTS 语言时),装饰器(Decorators) 是关键概念,用于声明组件、管理状态和定义交互逻辑。以下是常用关键字的详细介绍及代码示例:


1. @Entry

作用 :标记应用的主入口组件,一个页面有且仅有一个 @Entry
示例

typescript 复制代码
@Entry
@Component
struct Index {
  build() {
    Column() {
      Text('Hello HarmonyOS')
    }
  }
}

2. @Component

作用 :定义自定义组件,必须与 struct 结构体一起使用。
示例

typescript 复制代码
@Component
struct MyComponent {
  build() {
    Text('自定义组件')
  }
}

3. @State

作用 :组件内部的状态变量,变化时触发 UI 更新。
示例

typescript 复制代码
@Entry
@Component
struct Counter {
  @State count: number = 0

  build() {
    Column() {
      Text(`Count: ${this.count}`)
      Button('增加').onClick(() => {
        this.count++
      })
    }
  }
}

点击按钮时,count 变化会自动更新 Text 显示。


4. @Prop

作用 :子组件接收父组件的单向 数据,变化会更新子组件。
示例

typescript 复制代码
// 父组件
@Entry
@Component
struct Parent {
  @State parentCount: number = 10

  build() {
    Column() {
      Child({ count: this.parentCount }) // 传递数据
      Button('父修改').onClick(() => {
        this.parentCount++
      })
    }
  }
}

// 子组件
@Component
struct Child {
  @Prop count: number

  build() {
    Text(`子组件收到: ${this.count}`)
  }
}

父组件的 parentCount 变化会同步到子组件,但子组件无法直接修改 count


作用 :父子组件间的双向绑定 数据,任何一方修改都会同步。
示例

typescript 复制代码
// 父组件
@Entry
@Component
struct Parent {
  @State parentCount: number = 20

  build() {
    Column() {
      Child({ countLink: $parentCount }) // 使用 $ 传递引用
      Button('父修改').onClick(() => {
        this.parentCount++
      })
    }
  }
}

// 子组件
@Component
struct Child {
  @Link countLink: number

  build() {
    Button('子修改').onClick(() => {
      this.countLink++ // 修改会同步到父组件
    })
  }
}

父组件和子组件均可修改 countLink,数据双向同步。


6. @Provide 和 @Consume

作用 :跨层级组件共享数据,祖先组件提供数据(@Provide),后代组件消费数据(@Consume)。
示例

typescript 复制代码
// 祖先组件
@Entry
@Component
struct Grandparent {
  @Provide shareData: string = '全局数据'

  build() {
    Column() {
      Parent()
    }
  }
}

// 中间组件
@Component
struct Parent {
  build() {
    Child()
  }
}

// 后代组件
@Component
struct Child {
  @Consume shareData: string

  build() {
    Text(`收到数据: ${this.shareData}`)
  }
}

7. @Watch

作用 :监听状态变量变化,执行回调函数。
示例

typescript 复制代码
@Entry
@Component
struct WatchExample {
  @State count: number = 0
  @State log: string = ''

  @Watch('count') // 监听 count 变化
  watchCount() {
    this.log = `Count 变为 ${this.count}`
  }

  build() {
    Column() {
      Text(this.log)
      Button('增加').onClick(() => {
        this.count++
      })
    }
  }
}

8. @Builder

作用 :创建可复用的 UI 构建函数。
示例

typescript 复制代码
@Entry
@Component
struct BuilderExample {
  @Builder customButton(text: string) {
    Button(text)
      .width(100)
      .height(40)
      .backgroundColor(Color.Blue)
  }

  build() {
    Column() {
      this.customButton('按钮1')
      this.customButton('按钮2')
    }
  }
}

9. @Styles

作用 :定义可复用的样式集合。
示例

typescript 复制代码
@Entry
@Component
struct StyleExample {
  @Styles commonStyle() {
    .width(200)
    .height(50)
    .backgroundColor(Color.Green)
  }

  build() {
    Column() {
      Text('文本1').commonStyle()
      Button('按钮').commonStyle()
    }
  }
}

总结

  • 状态管理@State@Prop@Link@Provide/@Consume
  • UI 构建@Entry@Component@Builder@Styles
  • 副作用监听@Watch

合理使用这些关键字可以高效管理组件状态和数据流,构建响应式 UI。根据具体场景选择单向 (@Prop) 或双向 (@Link) 数据绑定,跨组件共享数据时考虑 @Provide/@Consume

相关推荐
IntMainJhy14 分钟前
【flutter for open harmony】第三方库「Flutter 聊天组件鸿蒙化适配与实战:从零搭建鸿蒙跨平台聊天页面」
flutter·华为·harmonyos
key_3_feng15 分钟前
HarmonyOS NEXT开发环境搭建深度方案
华为·harmonyos
苗俊祥20 分钟前
沐界浏览器-轻量 · 多标签 · 为鸿蒙设备打造的网页浏览体验*
华为·harmonyos·鸿蒙
jiejiejiejie_28 分钟前
Flutter for OpenHarmony 地图功能萌系实战指南:给 App 加上超萌 “小地图”✨
flutter·华为·harmonyos
南村群童欺我老无力.35 分钟前
鸿蒙网络请求的错误处理与超时管理
网络·华为·harmonyos
jiejiejiejie_39 分钟前
Flutter for OpenHarmony 页面导航与动效库适配小记复盘:让 App 又丝滑又灵动✨
flutter·华为·harmonyos
音视频牛哥1 小时前
鸿蒙NEXT 音视频推送端实践:基于SmartMediaKit实现RTMP推流、轻量级RTSP服务与实时录像
华为·harmonyos·smartmediakit·鸿蒙next rtmp推流·鸿蒙next 摄像头推流·鸿蒙采集摄像头推流·鸿蒙采集摄像头麦克风推rtmp
想你依然心痛1 小时前
HarmonyOS 6(API 23)实战:基于 Face AR & Body AR 的“空间交互设计工作台“——PC端手势驱动3D建模系统
ar·交互·harmonyos·悬浮导航·沉浸光感
liulian09161 小时前
Flutter 网络状态与内容分享库:connectivity_plus 与 share_plus 的 OpenHarmony 适配指南总结
flutter·华为·学习方法·harmonyos
IntMainJhy1 小时前
Flutter 引导页 Onboarding 第三方库 的鸿蒙化适配与实战指南
harmonyos