Name Last Update
..
modules Loading commit data...
README.md Loading commit data...
index.uts Loading commit data...
mapHelpers.uts Loading commit data...

uni-app x Store 模块使用说明

本目录包含 uni-app x 项目的状态管理实现。该方案基于 Vue 3 的 reactive 响应式系统,结合 Class (类) 定义状态结构,以满足 UTS 严格的类型系统要求,同时保持了与 Vuex 类似的 API (state, getters, dispatch, commit)。

目录结构

  • index.uts: 核心 Store 文件。定义了全局 State 类 (RootState)、初始状态、Getters 以及 dispatch/commit 方法。
  • mapHelpers.uts: 提供类似 Vuex 的辅助函数 (mapState, mapGetters, mapMutations),用于在 Options API 或 Vue 组件中简化状态访问。
  • modules/: 独立的功能模块(如 user.uts, audio.uts),每个模块通过 Class 定义其状态结构。

核心概念与变更

1. State (状态) - 基于 Class

为了解决 UTS 中对象字面量 ({}) 与接口/类型 (interface/type) 之间的兼容性问题(如 "Classifier does not have a companion object" 错误),所有 State 现在都通过 Class 定义,并使用 reactive(new StateClass()) 初始化。

模块定义示例 (store/modules/user.uts):

import { reactive } from "vue";

// 1. 定义 State 类 (必须包含 static _C 属性以规避编译错误)
export class UserStateType {
    static _C: string = "companion"; // 必须存在,解决 UTS 伴生对象报错
    token: string | null = null;
    userInfo: UTSJSONObject = {};
    isLoggedIn: boolean = false;
}

// 2. 初始化 State
export const state = reactive(new UserStateType());

// ... actions & getters

2. RootState (根状态)

RootState 在 store/index.uts 中定义为一个 Class,显式引用所有子模块的状态。

store/index.uts:

import { state as userState, UserStateType } from "@/store/modules/user.uts";
// ... 其他模块导入

export class RootState {
    user: UserStateType = userState;
    // ... 其他模块属性
}

export const state = reactive(new RootState());

3. Actions & Mutations

Actions 中修改 State 时,由于 UTS 的类型检查,可能需要对 Payload 进行显式类型转换,或者避免使用对象展开运算符 (...) 直接赋值给 Class 实例。

正确写法:

// store/modules/user.uts
export const actions = {
    setUserInfo: (payload: any): any | null => {
        // 显式转换类型
        const data = payload as UTSJSONObject;

        // 逐个属性赋值 (推荐,类型安全)
        const s = state as UserStateType;
        if (data['name'] != null) {
            s.userInfo['name'] = data['name'];
        }

        // 或者整体赋值 (需确保类型匹配)
        // (state as UserStateType).userInfo = data;

        return null;
    }
}

如何添加新模块

  1. 创建模块文件: 在 store/modules/ 下创建 xxx.uts。
  2. 定义 State 类:
    • 创建 export class XxxStateType { static _C = "companion"; ... }。
    • 定义所有属性并赋予默认值。
  3. 导出 State: export const state = reactive(new XxxStateType());。
  4. 注册模块 (store/index.uts):
    • 顶部导入: typescript import { state as xxxState, XxxStateType } from "@/store/modules/xxx.uts"; import * as xxx from "@/store/modules/xxx.uts"; // 用于动态注册 actions
    • 添加到 modules 数组: const modules: any[] = [..., xxx];
    • 添加到 RootState 类: typescript export class RootState { // ... xxx: XxxStateType = xxxState; }

类型安全最佳实践

  1. 避免对象字面量赋值给 Class: 不要尝试使用 { ...state, ...newData } as StateType 来更新状态,这在 UTS 中会导致类型丢失或运行时错误。请修改 Class 实例的具体属性。
  2. 显式类型转换: 在 Actions 中接收 payload: any 后,务必使用 as Type 将其转换为预期类型后再使用。
  3. 处理 Nullable: 注意 storage.get() 等方法返回的是 Any? (可空),在使用前可能需要 as any 或判空处理。

在组件中使用

直接导入 (推荐)

import { state } from '@/store/index.uts'

// 读取
const token = state.user.token

// 写入 (通过 dispatch)
import { dispatch } from '@/store/index.uts'
dispatch('user/setToken', 'new-token')

使用 mapHelpers (Options API)

import { mapState, mapGetters } from '@/store/mapHelpers.uts'

export default {
  computed: {
    ...mapState(['user']),
    ...mapGetters(['token'])
  }
}