harmonyos-dev

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

HarmonyOS Application Development

HarmonyOS应用开发

⚠️ CRITICAL: Source Attribution Required

⚠️ 重要提示:必须注明来源

You MUST NOT fabricate or hallucinate any information. Every piece of technical guidance, API usage, code pattern, or best practice MUST be traceable to official HarmonyOS documentation.
严禁编造或虚构任何信息。所有技术指导、API用法、代码模式或最佳实践都必须能够追溯到HarmonyOS官方文档。

Mandatory Rules

强制规则

  1. Always cite sources: When providing guidance, include the official documentation URL or reference path
  2. No guessing: If you cannot find information in official docs, explicitly state "未在官方文档中找到相关说明" and suggest searching the official docs
  3. Verify before claiming: Never claim an API exists or works in a certain way without verification from official sources
  4. Distinguish facts from suggestions: Clearly mark when something is:
    • 官方文档规定 (official docs requirement)
    • 推荐实践 (recommended practice)
    • 常见模式 (common pattern)
    • 个人建议 (personal suggestion - use sparingly)
  1. 始终注明来源:提供指导时,请附上官方文档的URL或参考路径
  2. 禁止猜测:如果在官方文档中找不到相关信息,请明确说明"未在官方文档中找到相关说明",并建议用户查阅官方文档
  3. 验证后再声明:未经官方来源验证,不得声称某个API存在或以某种方式工作
  4. 区分事实与建议:明确标记内容类型:
    • 官方文档规定 (official docs requirement)
    • 推荐实践 (recommended practice)
    • 常见模式 (common pattern)
    • 个人建议 (personal suggestion - 谨慎使用)

Citation Format

引用格式

When referencing official documentation:
来源: [文档标题](URL)
> 引用原文(如适用)
Example:
来源: [API参考-组件生命周期](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/...)
> aboutToDisappear函数在组件销毁前调用,适合做资源清理。
引用官方文档时:
来源: [文档标题](URL)
> 引用原文(如适用)
示例:
来源: [API参考-组件生命周期](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/...)
> aboutToDisappear函数在组件销毁前调用,适合做资源清理。

When Documentation is Unavailable

当文档不可用时

If you cannot find information in official docs:
  1. State clearly: "⚠️ 未在官方文档中找到关于 [topic] 的明确说明"
  2. Suggest user check: official docs, DevEco Studio hints, or community resources
  3. Do NOT fabricate or assume based on similar frameworks (React, Flutter, etc.)
如果在官方文档中找不到相关信息:
  1. 明确说明:"⚠️ 未在官方文档中找到关于 [topic] 的明确说明"
  2. 建议用户查看:官方文档、DevEco Studio提示或社区资源
  3. 不得基于类似框架(React、Flutter等)进行编造或假设

Pre-Development Checklist

开发前检查清单

Confirm before starting a new project or first-time development:
  • API version: Default API 20+; state explicitly if backward compatibility needed
  • Target device: PC / Phone / Tablet / Wearable / Multi-device
  • Scenario: Greenfield / Iteration / Architecture design
  • Project structure: HAP package layout, module organization
启动新项目或首次开发前请确认:
  • API版本:默认API 20+;如需向后兼容请明确说明
  • 目标设备:PC / 手机 / 平板 / 穿戴设备 / 多设备
  • 场景:全新项目 / 项目迭代 / 架构设计
  • 项目结构:HAP包布局、模块组织

Core Technology Stack

核心技术栈

ArkTS Language

ArkTS语言

  • TypeScript-based extension
  • State decorators:
    @ObservedV2
    ,
    @Trace
    ,
    @Local
    ,
    @Provider
  • Component decorators:
    @Component
    ,
    @ComponentV2
    ,
    @Entry
    ,
    @Builder
  • Rendering: on-demand updates, minimal re-renders
  • 基于TypeScript的扩展语言
  • 状态装饰器:
    @ObservedV2
    ,
    @Trace
    ,
    @Local
    ,
    @Provider
  • 组件装饰器:
    @Component
    ,
    @ComponentV2
    ,
    @Entry
    ,
    @Builder
  • 渲染机制:按需更新,最小化重渲染

ArkUI Component System

ArkUI组件系统

  • Declarative UI construction
  • Layout containers: Column / Row / Stack / Flex / Grid
  • List rendering: ForEach / LazyForEach
  • Animation: property animation, transition, shared element transition
  • 声明式UI构建
  • 布局容器:Column / Row / Stack / Flex / Grid
  • 列表渲染:ForEach / LazyForEach
  • 动画:属性动画、过渡动画、共享元素过渡

Common Patterns

常见模式

State Management

状态管理

  • Component-local:
    @Local
  • Cross-component:
    @Provider
    +
    @Consumer
  • Deep reactivity:
    @ObservedV2
    +
    @Trace
  • 组件本地状态:
    @Local
  • 跨组件状态:
    @Provider
    +
    @Consumer
  • 深度响应式:
    @ObservedV2
    +
    @Trace

Page Navigation

页面导航

  • Router.pushUrl()
    - push page
  • Router.replaceUrl()
    - replace page
  • Router.back()
    - go back
  • Router.pushUrl()
    - 跳转页面
  • Router.replaceUrl()
    - 替换页面
  • Router.back()
    - 返回上一页

Data Persistence

数据持久化

  • Preferences: lightweight key-value storage
  • RDB: relational database
  • Distributed data: cross-device sync
  • Preferences:轻量级键值存储
  • RDB:关系型数据库
  • 分布式数据:跨设备同步

Network

网络

  • @kit.NetworkKit
    http - HTTP requests
  • WebSocket - persistent connections
  • Upload/Download:
    @kit.BasicServicesKit
    request
  • @kit.NetworkKit
    http - HTTP请求
  • WebSocket:持久化连接
  • 上传/下载:
    @kit.BasicServicesKit
    请求

Official Documentation

官方文档

Primary Documentation Source (MANDATORY)

主要文档来源(强制要求)

Use context7 MCP tool to fetch the latest HarmonyOS documentation before providing any guidance.
undefined
提供任何指导前,请使用context7 MCP工具获取最新的HarmonyOS文档
undefined

Example workflow:

示例流程:

  1. Use mcp__context7__resolve-library-id with query "harmonyos arkts"
  2. Use mcp__context7__query-docs to fetch relevant documentation
  3. Cite the documentation source in your response
undefined
  1. 使用 mcp__context7__resolve-library-id 查询 "harmonyos arkts"
  2. 使用 mcp__context7__query-docs 获取相关文档
  3. 在回复中引用文档来源
undefined

Official Documentation Links

官方文档链接

Documentation Query Protocol

文档查询流程

When answering HarmonyOS development questions:
  1. First: Query context7 for relevant documentation
  2. Then: If context7 doesn't have the answer, search official docs links above
  3. Finally: If no official documentation found, explicitly state:
    ⚠️ 未在官方文档中找到关于 [topic] 的明确说明,建议查阅:
  4. Never fabricate API signatures, method names, or behavior patterns
回答HarmonyOS开发问题时:
  1. 首先:使用context7查询相关文档
  2. 其次:如果context7没有答案,搜索上述官方文档链接
  3. 最后:如果未找到官方文档,请明确说明:
    ⚠️ 未在官方文档中找到关于 [topic] 的明确说明,建议查阅:
  4. 严禁编造API签名、方法名称或行为模式

References

参考资料

Load detailed docs as needed (all include source citations):
  • State management: references/state-management.md
  • UI components: references/ui-components.md
  • Data persistence: references/data-persistence.md
  • Network: references/network.md
  • Permissions: references/permissions.md
  • Performance: references/performance.md
根据需要加载详细文档(均包含来源引用):
  • 状态管理references/state-management.md
  • UI组件references/ui-components.md
  • 数据持久化references/data-persistence.md
  • 网络references/network.md
  • 权限references/permissions.md
  • 性能references/performance.md