hmos-arkui-mvvm-pattern

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

ArkUI MVVM Architecture Pattern

ArkUI MVVM 架构模式

Definition

定义

FieldContent
skill_id
arkui-mvvm-pattern
skill_name
ArkUI MVVM Architecture Pattern
one_line_purpose
Develop or refactor MVVM implementations for V1 and V2 state-management versions
device_scope
phone / tablet / 2in1
problem_scope
MVVM three-layer separation: View, Model, ViewModel
not_in_scope
V1/V2 decorator usage and non-MVVM architecture patterns
primary_outputs
primary_scene
,
implementation_notes
,
code_touchpoints
,
verification_matrix
字段内容
skill_id
arkui-mvvm-pattern
skill_name
ArkUI MVVM 架构模式
one_line_purpose
为 V1 和 V2 状态管理版本开发或重构 MVVM 实现
device_scope
手机 / 平板 / 2in1
problem_scope
MVVM 三层分离:View、Model、ViewModel
not_in_scope
V1/V2 装饰器用法及非 MVVM 架构模式
primary_outputs
primary_scene
,
implementation_notes
,
code_touchpoints
,
verification_matrix

Core Constraints

核心约束

  1. Keep the current project version: unless migration to V2 is explicit, keep the current state-management version and do not mix V1 and V2.
  2. Unidirectional data flow (UDF): data flows downward Model → ViewModel → View; events flow upward View → ViewModel → Model.
  3. Single source of truth (SSOT): data changes occur in the data layer; ViewModel is the sole source of UI state.
  4. View must not access Model directly: access it through ViewModel.
  5. Minimize AppStorage: use AppStorage/AppStorageV2 only when necessary; ViewModel should hold state directly.
  6. V1
    @Observed
    +
    @Track
    cannot coexist with getters
    : a V1
    @Observed
    class with
    @Track
    properties must not define a
    get
    accessor, or runtime failure may occur.
  7. Access data through ViewModel types: View/Page must not import Model types. Define an
    @Observed
    ViewModel class for each list item and convert Model → ViewModel there.
  1. 保持当前项目版本:除非明确要求迁移到 V2,否则应保持当前的状态管理版本,不得混用 V1 和 V2。
  2. 单向数据流(UDF):数据向下流动,即 Model → ViewModel → View;事件向上流动,即 View → ViewModel → Model。
  3. 单一数据源(SSOT):数据变更发生在数据层;ViewModel 是 UI 状态的唯一来源。
  4. View 不得直接访问 Model:必须通过 ViewModel 访问 Model。
  5. 尽量减少使用 AppStorage:仅在必要时使用 AppStorage/AppStorageV2;ViewModel 应直接持有状态。
  6. V1 的
    @Observed
    +
    @Track
    不能与 getter 共存
    :带有
    @Track
    属性的 V1
    @Observed
    类不得定义
    get
    访问器,否则可能在运行时发生故障。
  7. 通过 ViewModel 类型访问数据:View/Page 不得导入 Model 类型。为每个列表项定义一个
    @Observed
    ViewModel 类,并在其中完成 Model → ViewModel 转换。

Keep the Current Version

保持当前版本

Determine whether the project uses V1 or V2 before acting. Search an
.ets
file for
@Component
or
@ComponentV2
; if both occur, choose one version and standardize it.
采取操作前,先确定项目使用的是 V1 还是 V2。在一个
.ets
文件中搜索
@Component
或
@ComponentV2
;如果两者同时出现,则选择一种版本并统一为该版本。

State 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.
  1. If a value can be computed from existing properties, do not store it; use
    @Computed
    (V2) or a getter (V1).
  2. If only one component needs independent data, keep it private with
    @Local
    (V2) or
    @State
    (V1).
  3. If several components need UI-only state, lift it to their nearest common ancestor and pass it with
    @Param
    (V2) or
    @Prop
    (V1).
  4. If it involves business logic or data access, make it a ViewModel property using
    @Trace
    (V2) or
    @Track
    (V1).
PrincipleDescriptionAvoid
Do not store derived stateCompute values with
@Computed
or a getter
Store
allowClick
and synchronize it manually
Keep state localPut state in the smallest required scopePut one-component
isShowSheet
in ViewModel
Single sourceManage each value in one placeStore
inputContent
in both Page and ViewModel
Lift to common ancestorShare state at the nearest common ancestorPut sibling-only
selectedIndex
directly in ViewModel
这些原则同时适用于新开发和重构。这通常称为 State Hoisting:让状态靠近其使用位置,仅在必要时提升状态。
  1. 如果某个值可以通过现有属性计算得出,则不要存储它;使用
    @Computed
    (V2)或 getter(V1)。
  2. 如果只有一个组件需要独立数据,应使用
    @Local
    (V2)或
    @State
    (V1)将其保持为私有状态。
  3. 如果多个组件需要仅用于 UI 的状态,将其提升到最近的共同祖先,并通过
    @Param
    (V2)或
    @Prop
    (V1)传递。
  4. 如果状态涉及业务逻辑或数据访问,将其设为使用
    @Trace
    (V2)或
    @Track
    (V1)的 ViewModel 属性。
原则说明应避免
不存储派生状态使用
@Computed
或 getter 计算值
存储
allowClick
并手动同步
保持状态局部化将状态置于所需的最小作用域将仅供单个组件使用的
isShowSheet
放入 ViewModel
单一数据源在一个位置管理每个值同时在 Page 和 ViewModel 中存储
inputContent
提升到共同祖先在最近的共同祖先中共享状态将仅供兄弟组件使用的
selectedIndex
直接放入 ViewModel

Code File Principles

代码文件原则

  1. Business data entities belong in Model (entity classes and Repository).
  2. UI state that drives rendering belongs in ViewModel (
    @Observed
    /
    @ObservedV2
    ).
  3. Pure logic and system-capability wrappers belong in
    utils/
    .
  1. 业务数据实体应放在 Model 中(实体类和 Repository)。
  2. 驱动渲染的 UI 状态应放在 ViewModel 中(
    @Observed
    /
    @ObservedV2
    )。
  3. 纯逻辑和系统能力包装应放在
    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 components
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 components

Workflow

工作流

Follow the workflow strictly. For new pages or modules, use references/add-func-workflow.md. For existing code, use references/refactor-func-workflow.md.
TagPhaseFocus
REQ
Requirements and designArchitecture, layering, state ownership, V1/V2 selection
DEV
DevelopmentModel/ViewModel/View/Page implementation, decorators, data flow
VAL
ValidationData-flow compliance, Model isolation, decorator pairing, refresh behavior
严格遵循以下工作流。对于新页面或模块,使用 references/add-func-workflow.md。对于现有代码,使用 references/refactor-func-workflow.md。
标签阶段关注点
REQ
需求与设计架构、分层、状态归属、V1/V2 选择
DEV
开发Model/ViewModel/View/Page 实现、装饰器、数据流
VAL
验证数据流合规性、Model 隔离、装饰器配对、刷新行为

Scene Index

场景索引

MVVM-01
V1 MVVM Development

MVVM-01
V1 MVVM 开发

yaml
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_02
yaml
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_02

MVVM-02
V2 MVVM Development

MVVM-02
V2 MVVM 开发

yaml
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_04
yaml
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_04

Reference Resources

参考资源

FileContentLoad when
references/anti-patterns.mdArchitecture anti-patterns and scan checklistScanning an existing project
references/v1-nested-observation.mdV1 nested object and array observationHandling nested V1 observation
references/v1-v2-mapping.mdV1/V2 array behavior and decorator pairingHandling array updates or mixed versions
references/v2-advanced.mdAppStorageV2/PersistenceV2 signatures and selectionUsing V2 global state
references/multi-module.mdThree-layer architecture, modules, and devicesMulti-module or multi-device projects
文件内容加载时机
references/anti-patterns.md架构反模式和扫描检查清单扫描现有项目时
references/v1-nested-observation.mdV1 嵌套对象和数组观测处理嵌套 V1 观测时
references/v1-v2-mapping.mdV1/V2 数组行为和装饰器配对处理数组更新或混合版本时
references/v2-advanced.mdAppStorageV2/PersistenceV2 签名与选择使用 V2 全局状态时
references/multi-module.md三层架构、模块和设备多模块或多设备项目

Build Verification

构建验证

Before building, run
devecocli --version
. Use
devecocli build
for modified
.ets
files, repair errors, and run a full build when needed. If the CLI is unavailable, confirm with the user before installing it.
构建前,运行
devecocli --version
。对修改后的
.ets
文件使用
devecocli build
,修复错误,并在需要时执行完整构建。如果 CLI 不可用,请先向用户确认后再安装。