pinia

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Pinia

Pinia

Pinia is the official state management library for Vue, designed to be intuitive and type-safe. It supports both Options API and Composition API styles, with first-class TypeScript support and devtools integration.
The skill is based on Pinia v3.0.4, generated at 2026-01-28.
Pinia是Vue官方的状态管理库,设计直观且类型安全。它同时支持Options API和Composition API风格,提供一流的TypeScript支持以及开发者工具集成。
本技能基于Pinia v3.0.4,生成于2026年1月28日。

Core References

核心参考

TopicDescriptionReference
StoresDefining stores, state, getters, actions, storeToRefs, subscriptionscore-stores
主题描述参考链接
Stores定义store、state、getters、actions,storeToRefs,订阅core-stores

Features

功能特性

Extensibility

可扩展性

TopicDescriptionReference
PluginsExtend stores with custom properties, state, and behaviorfeatures-plugins
主题描述参考链接
Plugins使用自定义属性、状态和行为扩展storefeatures-plugins

Composability

组合性

TopicDescriptionReference
ComposablesUsing Vue composables within stores (VueUse, etc.)features-composables
Composing StoresStore-to-store communication, avoiding circular dependenciesfeatures-composing-stores
主题描述参考链接
Composables在store中使用Vue组合式函数(如VueUse等)features-composables
Composing StoresStore间通信,避免循环依赖features-composing-stores

Best Practices

最佳实践

TopicDescriptionReference
TestingUnit testing with @pinia/testing, mocking, stubbingbest-practices-testing
Outside ComponentsUsing stores in navigation guards, plugins, middlewaresbest-practices-outside-component
主题描述参考链接
Testing使用@pinia/testing进行单元测试、模拟、存根best-practices-testing
Outside Components在导航守卫、插件、中间件中使用storebest-practices-outside-component

Advanced

进阶内容

TopicDescriptionReference
SSRServer-side rendering, state hydrationadvanced-ssr
NuxtNuxt integration, auto-imports, SSR best practicesadvanced-nuxt
HMRHot module replacement for developmentadvanced-hmr
主题描述参考链接
SSR服务端渲染、状态水合advanced-ssr
NuxtNuxt集成、自动导入、SSR最佳实践advanced-nuxt
HMR开发环境下的热模块替换advanced-hmr

Key Recommendations

关键建议

  • Prefer Setup Stores for complex logic, composables, and watchers
  • Use
    storeToRefs()
    when destructuring state/getters to preserve reactivity
  • Actions can be destructured directly - they're bound to the store
  • Call stores inside functions not at module scope, especially for SSR
  • Add HMR support to each store for better development experience
  • Use
    @pinia/testing
    for component tests with mocked stores
  • 优先使用Setup Stores处理复杂逻辑、组合式函数和监听器
  • **使用
    storeToRefs()
    **解构state/getters时保留响应式
  • Actions可直接解构 - 它们已绑定到store
  • 在函数内部调用store而非模块作用域,尤其是在SSR场景下
  • 为每个store添加HMR支持以提升开发体验
  • **使用
    @pinia/testing
    **在组件测试中模拟store