Pinia 实现模块化数据共享的核心是通过 storeToRefs 保持响应式、$subscribe 监听状态变更、defineStore 抽离共享逻辑层,并谨慎使用 $state 跨模块写入。
模块化 Store 实现数据共享,核心在于打破模块边界,让状态可被安全、可控地跨模块读取与响应。Vue 3 的 Pinia 是目前最推荐的方案,它天然支持模块化,且通过 storeToRefs、getActivePinia() 和 defineStore 的灵活组合,能优雅解决跨模块访问问题。
直接解构 store 属性会丢失响应性,这是常见陷阱。正确做法是先用 storeToRefs 提取响应式引用,再在其他模块中使用:
userInfo 和 isLoggedIn
const { isLoggedIn } = storeToRefs(useUserStore())
isLoggedIn 的读取或 watch 都能自动响应变化当某个模块需要“感知”另一模块的状态更新(比如用户登出时清空购物车),可用 $subscribe 订阅目标 store 的变更:
useUserStore().$subscribe((mutation) => { if (mutation.storeId === 'user' && mutation.type === 'patch object' && !mutation.payload.isLoggedIn) clearCart() })
unsubscribe 或使用 onBeforeUnmount 清理对于多个模块共用的计算逻辑或派生状态(如权限判断、全局 loading 状态),建议单独抽离为一个“逻辑 store”,不存放原始数据,只封装方法和 getters:
sharedLogic.ts,用 defineStore('shared', () => { const user = useUserStore(); const cart = useCartStore(); return { canCheckout: computed(() => user.isLoggedIn && cart.items.length > 0), isLoading: computed(() => user.$state.loading || cart.$state.loading) } })
sharedLogic.canCheckout,无需重复判断虽然技术上可通过 useOtherStore().$state.xxx = newValue 修改其他模块状态,但强烈建议仅限于极少数场景(如全局错误重置、主题切换):
userStore.logoutAndResetAll() 内部统一清理 user、cart、notification 等相关状态模块间数据共享不是越自由越好,而是要在解耦与协同之间找平衡。用好 Pinia 的响应式穿透能力、订阅机制和逻辑抽象,比强行“打通所有模块”更可持续。