pinia-skilld
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesevuejs/pinia pinia
piniavuejs/pinia pinia
piniaVersion: 3.0.4 (Nov 2025)
Deps: @vue/devtools-api@^7.7.7
Tags: next: 2.0.0-rc.10 (Sep 2021), beta: 2.1.8-beta.0 (Apr 2024), latest: 3.0.4 (Nov 2025)
References: Docs — API reference, guides • GitHub Issues — bugs, workarounds, edge cases • GitHub Discussions — Q&A, patterns, recipes • Releases — changelog, breaking changes, new APIs
**版本:**3.0.4(2025年11月)
依赖:@vue/devtools-api@^7.7.7
**标签:**next: 2.0.0-rc.10(2021年9月),beta: 2.1.8-beta.0(2024年4月),latest: 3.0.4(2025年11月)
参考链接:文档 — API参考、使用指南 • GitHub Issues — 问题反馈、解决方案、边缘案例 • GitHub Discussions — 问答、模式、实践方案 • 版本发布记录 — 更新日志、破坏性变更、新API
API Changes
API变更
This section documents version-specific API changes — prioritize recent major/minor releases.
-
BREAKING:— object signature with
defineStore({ id: 'storeName', ... })property removed in v3.0.0; useidinstead. Old object syntax silently compiled but is now a runtime error sourcedefineStore('storeName', { ... }) -
BREAKING:type — removed in v3.0.0; use
PiniaStorePlugininstead. Code usingPiniaPluginwill fail to compile sourcePiniaStorePlugin -
BREAKING: Vue 2 support dropped in v3.0.0 — Pinia v3 requires Vue 3 only. Users on Vue 2 must stay on Pinia v2 source
-
BREAKING: TypeScript 5 or newer required in v3.0.0 — uses nativetype introduced in TS 4.5; TS 5+ recommended source
Awaited -
BREAKING: IIFE bundle no longer bundles Vue Devtools in v3.0.0 — devtools API was too large; must be included manually depending on your workflow source
-
BREAKING: Package is now published asin v3.0.0 — CJS dist files still provided but the package root is ESM. May break setups relying on implicit CJS resolution source
type: module -
NEW:helper in setup stores — added in v2.2.0, available via
action(fn, name?)parameter. Wraps a function so it is tracked bySetupStoreHelperswhen called within the store; intended for advanced use cases like Pinia Colada source$onAction -
NEW:— added in v2.1.7, stops the pinia effect scope and removes state, plugins, and stores. Useful in tests or multi-pinia apps; disposed instance cannot be reused source
disposePinia(pinia) -
NEW:type — added in v2.1.7 for the return type of
SetupStoreDefinition<Id, SS>when using a setup function. ExtendsdefineStore()and enables better IDE support for setup stores sourceStoreDefinition -
NEW:now picks up writable
mapWritableStates in setup stores — added in v2.3.0. Previously onlycomputedstate was mapped;refreturns from setup stores are now included sourceWritableComputedRef
Also changed: DEPRECATED (alias for , still exported) · returns (typed more strictly since v2.0.35) · stable — skips SSR hydration for non-state objects returned from setup stores · exported utility for plugin authors
mapGettersmapStategetActivePinia()Pinia | undefinedskipHydrate(obj)shouldHydrate(obj)本节记录特定版本的API变更 — 优先关注近期的主版本/次版本更新。
-
破坏性变更:— 带
defineStore({ id: 'storeName', ... })属性的对象签名在v3.0.0中已移除;请改用id。旧的对象语法之前会静默编译,但现在会触发运行时错误 来源defineStore('storeName', { ... }) -
破坏性变更:类型 — 在v3.0.0中已移除;请改用
PiniaStorePlugin。使用PiniaPlugin的代码将无法编译 来源PiniaStorePlugin -
破坏性变更:v3.0.0中移除了Vue 2支持 — Pinia v3仅兼容Vue 3。使用Vue 2的用户必须停留在Pinia v2版本 来源
-
破坏性变更:v3.0.0要求使用TypeScript 5或更高版本 — 使用了TS 4.5引入的原生类型;推荐使用TS 5+ 来源
Awaited -
破坏性变更:v3.0.0中的IIFE包不再捆绑Vue Devtools — Devtools API体积过大;需根据你的工作流手动引入 来源
-
破坏性变更:v3.0.0中包以形式发布 — 仍提供CJS分发文件,但包根目录为ESM。可能会破坏依赖隐式CJS解析的配置 来源
type: module -
新增:setup store中的工具函数 — 在v2.2.0中添加,可通过
action(fn, name?)参数获取。用于包装函数,使其在store内部调用时能被SetupStoreHelpers追踪;适用于Pinia Colada等高级场景 来源$onAction -
新增:— 在v2.1.7中添加,用于停止pinia的作用域并移除状态、插件和store。在测试或多pinia应用中非常实用;已销毁的实例无法重复使用 来源
disposePinia(pinia) -
新增:类型 — 在v2.1.7中添加,作为使用setup函数时
SetupStoreDefinition<Id, SS>的返回类型。扩展了defineStore(),为setup store提供更好的IDE支持 来源StoreDefinition -
新增:v2.3.0中现在可识别setup store中的可写
mapWritableState— 之前仅能映射computed状态;现在setup store返回的ref也会被包含在内 来源WritableComputedRef
其他变更:已废弃(作为的别名,仍会导出)· 返回(自v2.0.35起类型更严格)· 已稳定 — 用于跳过setup store返回的非状态对象的SSR hydration · 为插件作者导出了工具函数
mapGettersmapStategetActivePinia()Pinia | undefinedskipHydrate(obj)shouldHydrate(obj)Best Practices
最佳实践
-
Usewith a function callback rather than an object when mutating arrays or performing multiple related changes — the function form groups all mutations into a single devtools entry and avoids creating intermediate collections source
$patch() -
Useinstead of
$subscribe()on store state — subscriptions fire only once perwatch()call regardless of how many individual properties changed, avoiding redundant callbacks when using the function form of$patchsource$patch -
Passto
{ detached: true }and$subscribe()as the second arg totruewhen you need listeners to outlive the component — by default both are automatically removed on component unmount source$onAction() -
In setup stores, useto wrap state properties that must not be picked up from SSR initial state (e.g., composables backed by
skipHydrate(), client-only refs) — without it, the server's serialized value will override the intended client-side source sourcelocalStorage
ts
return {
lastColor: skipHydrate(lastColor), // won't be overwritten by SSR state
open,
}-
When a plugin adds new state properties, set the value on both(for SSR serialization and devtools) and
store.$stateviastore— setting only one breaks devtools display or reactivity sharing sourcetoRef(store.$state, 'key') -
Wrap non-reactive external objects (router, class instances, third-party lib instances) withbefore assigning them in plugins — prevents Vue from trying to deeply observe objects that aren't meant to be reactive source
markRaw() -
When composing stores that reference each other, placecalls before any
useOtherStore()in async actions — after anawaitthe active pinia context may have changed, causing the wrong instance to be returned in SSR sourceawait -
Usefrom
createTestingPinia()for component tests rather than@pinia/testing— it stubs all actions by default and makes them inspectable as spies; pass plugins via thecreatePinia()option, not viapluginssourcetestingPinia.use() -
In options store getters, prefer arrow functions that receiveas the first parameter over regular functions using
state— arrow function return types are inferred automatically, whilethis-based getters require an explicit return type annotation sourcethis
-
当修改数组或执行多个相关变更时,优先使用带函数回调的而非对象形式 — 函数形式会将所有变更合并为一个devtools条目,避免创建中间集合 来源
$patch() -
优先使用而非
$subscribe()监听store状态 — 无论有多少个属性变更,订阅仅会在每次watch()调用时触发一次,避免使用函数形式$patch时出现冗余回调 来源$patch -
当需要监听器在组件销毁后仍保持生效时,给传入
$subscribe(),给{ detached: true }的第二个参数传入$onAction()— 默认情况下,两者都会在组件卸载时自动移除 来源true -
在setup store中,使用包装不需要从SSR初始状态获取的状态属性(例如基于
skipHydrate()的composable、仅客户端的ref) — 若不使用该函数,服务器的序列化值会覆盖预期的客户端数据源 来源localStorage
ts
return {
lastColor: skipHydrate(lastColor), // won't be overwritten by SSR state
open,
}-
当插件添加新的状态属性时,需同时在(用于SSR序列化和devtools)和通过
store.$state在toRef(store.$state, 'key')上设置值 — 仅设置其中一个会破坏devtools显示或响应式共享 来源store -
在插件中分配非响应式外部对象(路由、类实例、第三方库实例)前,先用包装 — 避免Vue尝试深度观察非响应式对象 来源
markRaw() -
当组合相互引用的store时,在异步action中的任何之前调用
await—useOtherStore()之后活跃的pinia上下文可能会改变,导致在SSR中返回错误的实例 来源await -
组件测试中使用中的
@pinia/testing而非createTestingPinia()— 它会默认存根所有action,并将其作为可检查的间谍;通过createPinia()选项传入插件,而非plugins来源testingPinia.use() -
在options store的getter中,优先使用以为第一个参数的箭头函数而非使用
state的普通函数 — 箭头函数的返回类型会被自动推断,而基于this的getter需要显式的返回类型注解 来源this