hmos-arkui-mvvm-pattern
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseArkUI MVVM Architecture Pattern
ArkUI MVVM 架构模式
Definition
定义
| Field | Content |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| 字段 | 内容 |
|---|---|
| |
| |
| |
| |
| |
| |
| |
Core Constraints
核心约束
- Keep the current project version: unless migration to V2 is explicit, keep the current state-management version and do not mix V1 and V2.
- Unidirectional data flow (UDF): data flows downward Model → ViewModel → View; events flow upward View → ViewModel → Model.
- Single source of truth (SSOT): data changes occur in the data layer; ViewModel is the sole source of UI state.
- View must not access Model directly: access it through ViewModel.
- Minimize AppStorage: use AppStorage/AppStorageV2 only when necessary; ViewModel should hold state directly.
- V1 +
@Observedcannot coexist with getters: a V1@Trackclass with@Observedproperties must not define a@Trackaccessor, or runtime failure may occur.get - Access data through ViewModel types: View/Page must not import Model types. Define an ViewModel class for each list item and convert Model → ViewModel there.
@Observed
- 保持当前项目版本:除非明确要求迁移到 V2,否则应保持当前的状态管理版本,不得混用 V1 和 V2。
- 单向数据流(UDF):数据向下流动,即 Model → ViewModel → View;事件向上流动,即 View → ViewModel → Model。
- 单一数据源(SSOT):数据变更发生在数据层;ViewModel 是 UI 状态的唯一来源。
- View 不得直接访问 Model:必须通过 ViewModel 访问 Model。
- 尽量减少使用 AppStorage:仅在必要时使用 AppStorage/AppStorageV2;ViewModel 应直接持有状态。
- V1 的 +
@Observed不能与 getter 共存:带有@Track属性的 V1@Track类不得定义@Observed访问器,否则可能在运行时发生故障。get - 通过 ViewModel 类型访问数据:View/Page 不得导入 Model 类型。为每个列表项定义一个 ViewModel 类,并在其中完成 Model → ViewModel 转换。
@Observed
Keep the Current Version
保持当前版本
Determine whether the project uses V1 or V2 before acting. Search an file for or ; if both occur, choose one version and standardize it.
.ets@Component@ComponentV2采取操作前,先确定项目使用的是 V1 还是 V2。在一个 文件中搜索 或 ;如果两者同时出现,则选择一种版本并统一为该版本。
.ets@Component@ComponentV2State Variable Principles
状态变量原则
Apply these principles to both new development and refactoring. This is commonly called State Hoisting: keep state close to its use and lift it only as needed.
- If a value can be computed from existing properties, do not store it; use (V2) or a getter (V1).
@Computed - If only one component needs independent data, keep it private with (V2) or
@Local(V1).@State - If several components need UI-only state, lift it to their nearest common ancestor and pass it with (V2) or
@Param(V1).@Prop - If it involves business logic or data access, make it a ViewModel property using (V2) or
@Trace(V1).@Track
| Principle | Description | Avoid |
|---|---|---|
| Do not store derived state | Compute values with | Store |
| Keep state local | Put state in the smallest required scope | Put one-component |
| Single source | Manage each value in one place | Store |
| Lift to common ancestor | Share state at the nearest common ancestor | Put sibling-only |
这些原则同时适用于新开发和重构。这通常称为 State Hoisting:让状态靠近其使用位置,仅在必要时提升状态。
- 如果某个值可以通过现有属性计算得出,则不要存储它;使用 (V2)或 getter(V1)。
@Computed - 如果只有一个组件需要独立数据,应使用 (V2)或
@Local(V1)将其保持为私有状态。@State - 如果多个组件需要仅用于 UI 的状态,将其提升到最近的共同祖先,并通过 (V2)或
@Param(V1)传递。@Prop - 如果状态涉及业务逻辑或数据访问,将其设为使用 (V2)或
@Trace(V1)的 ViewModel 属性。@Track
| 原则 | 说明 | 应避免 |
|---|---|---|
| 不存储派生状态 | 使用 | 存储 |
| 保持状态局部化 | 将状态置于所需的最小作用域 | 将仅供单个组件使用的 |
| 单一数据源 | 在一个位置管理每个值 | 同时在 Page 和 ViewModel 中存储 |
| 提升到共同祖先 | 在最近的共同祖先中共享状态 | 将仅供兄弟组件使用的 |
Code File Principles
代码文件原则
- Business data entities belong in Model (entity classes and Repository).
- UI state that drives rendering belongs in ViewModel (/
@Observed).@ObservedV2 - Pure logic and system-capability wrappers belong in .
utils/
- 业务数据实体应放在 Model 中(实体类和 Repository)。
- 驱动渲染的 UI 状态应放在 ViewModel 中(/
@Observed)。@ObservedV2 - 纯逻辑和系统能力包装应放在 中。
utils/
Recommended Directory
推荐目录
text
ets/
├── model/ # Data models + Repository
├── viewmodel/ # View models
├── views/ # Business components
├── pages/ # Page entry points
├── utils/ # Pure logic and system-capability wrappers
└── common/ # Shared constants and componentstext
ets/
├── model/ # Data models + Repository
├── viewmodel/ # View models
├── views/ # Business components
├── pages/ # Page entry points
├── utils/ # Pure logic and system-capability wrappers
└── common/ # Shared constants and componentsWorkflow
工作流
Follow the workflow strictly. For new pages or modules, use references/add-func-workflow.md. For existing code, use references/refactor-func-workflow.md.
| Tag | Phase | Focus |
|---|---|---|
| Requirements and design | Architecture, layering, state ownership, V1/V2 selection |
| Development | Model/ViewModel/View/Page implementation, decorators, data flow |
| Validation | Data-flow compliance, Model isolation, decorator pairing, refresh behavior |
严格遵循以下工作流。对于新页面或模块,使用 references/add-func-workflow.md。对于现有代码,使用 references/refactor-func-workflow.md。
| 标签 | 阶段 | 关注点 |
|---|---|---|
| 需求与设计 | 架构、分层、状态归属、V1/V2 选择 |
| 开发 | Model/ViewModel/View/Page 实现、装饰器、数据流 |
| 验证 | 数据流合规性、Model 隔离、装饰器配对、刷新行为 |
Scene Index
场景索引
MVVM-01
V1 MVVM Development
MVVM-01MVVM-01
V1 MVVM 开发
MVVM-01yaml
scene_id: MVVM-01
name: V1 state-management MVVM development
phases: [REQ, DEV, VAL]
signals: [V1 state management, MVVM, architecture layers, Model/ViewModel/View]
not_when: V2 state management (MVVM-02) / non-MVVM architecture
ref: RSC_MVVM_01, RSC_MVVM_02yaml
scene_id: MVVM-01
name: V1 state-management MVVM development
phases: [REQ, DEV, VAL]
signals: [V1 state management, MVVM, architecture layers, Model/ViewModel/View]
not_when: V2 state management (MVVM-02) / non-MVVM architecture
ref: RSC_MVVM_01, RSC_MVVM_02MVVM-02
V2 MVVM Development
MVVM-02MVVM-02
V2 MVVM 开发
MVVM-02yaml
scene_id: MVVM-02
name: V2 state-management MVVM development
phases: [REQ, DEV, VAL]
signals: [V2 state management, MVVM, architecture layers, Model/ViewModel/View]
not_when: V1 state management (MVVM-01) / non-MVVM architecture
ref: RSC_MVVM_03, RSC_MVVM_04yaml
scene_id: MVVM-02
name: V2 state-management MVVM development
phases: [REQ, DEV, VAL]
signals: [V2 state management, MVVM, architecture layers, Model/ViewModel/View]
not_when: V1 state management (MVVM-01) / non-MVVM architecture
ref: RSC_MVVM_03, RSC_MVVM_04Reference Resources
参考资源
| File | Content | Load when |
|---|---|---|
| references/anti-patterns.md | Architecture anti-patterns and scan checklist | Scanning an existing project |
| references/v1-nested-observation.md | V1 nested object and array observation | Handling nested V1 observation |
| references/v1-v2-mapping.md | V1/V2 array behavior and decorator pairing | Handling array updates or mixed versions |
| references/v2-advanced.md | AppStorageV2/PersistenceV2 signatures and selection | Using V2 global state |
| references/multi-module.md | Three-layer architecture, modules, and devices | Multi-module or multi-device projects |
| 文件 | 内容 | 加载时机 |
|---|---|---|
| references/anti-patterns.md | 架构反模式和扫描检查清单 | 扫描现有项目时 |
| references/v1-nested-observation.md | V1 嵌套对象和数组观测 | 处理嵌套 V1 观测时 |
| references/v1-v2-mapping.md | V1/V2 数组行为和装饰器配对 | 处理数组更新或混合版本时 |
| references/v2-advanced.md | AppStorageV2/PersistenceV2 签名与选择 | 使用 V2 全局状态时 |
| references/multi-module.md | 三层架构、模块和设备 | 多模块或多设备项目 |
Build Verification
构建验证
Before building, run . Use for modified files, repair errors, and run a full build when needed. If the CLI is unavailable, confirm with the user before installing it.
devecocli --versiondevecocli build.ets构建前,运行 。对修改后的 文件使用 ,修复错误,并在需要时执行完整构建。如果 CLI 不可用,请先向用户确认后再安装。
devecocli --version.etsdevecocli build