Mpx 内置的 Vuex 风格 store(在 @mpxjs/core 中被 re-export),跑在 Mpx 自有响应式上。新项目推荐使用 @mpxjs/pinia,本包用于历史项目兼容。
- src/index.js:核心实现,导出
createStore/createStoreWithThis、辅助函数与Store类。 - src/mapStore.js:
mapState/mapGetters/mapMutations/mapActions帮助函数。 - @types/index.d.ts:对外类型。
- src/index.js 内主要分两部分:
transformGetters:把用户传入的 getters 包装成对当前 module/state 的代理函数,注入响应式(reactive+computed)。Store类:commit/dispatch/getters/state/subscribe,支持 module 嵌套与withThis风格。
- src/mapStore.js:四个 map 帮助函数把 store 字段映射到组件 computed/methods。
- 创建:用户
createStore({ state, mutations, actions, getters, modules })→ 内部基于@mpxjs/core的reactive/computed/effectScope构造Store实例。 - 挂载:通过
mpx.use(store)或createApp({ store })注入,组件实例上可通过this.$store访问。 - 读取/变更:
store.state.x/store.getters.y(响应式追踪) →commit('mutation')同步变更 →dispatch('action')异步流程 → 通过subscribe派发订阅。
- 新项目优先用 @mpxjs/pinia,本包仅做兼容维护。
- 响应式 API 全部从
@mpxjs/core引入,不要直接依赖 Vue。 - 与
@mpxjs/utils的proxy/getByPath/warn配合做路径访问与告警。