pinia-skilld by harlan-zw/vue-ecosystem-skills
npx skills add https://github.com/harlan-zw/vue-ecosystem-skills --skill pinia-skilldpinia直观、类型安全且灵活的 Vue 状态管理库
版本: 3.0.4 依赖: @vue/devtools-api@^7.7.7 标签: next: 2.0.0-rc.10, beta: 2.1.8-beta.0, latest: 3.0.4
参考: 文档 — API 参考、指南 • GitHub Issues — 错误、变通方案、边界情况 • GitHub Discussions — 问答、模式、配方 • 发布说明 — 更新日志、重大变更、新 API
本节记录了特定版本的 API 变更 — 请优先关注近期的主要/次要版本发布。
重大变更: defineStore({ id: 'storeName', ... }) — 在 v3.0.0 中移除了包含 id 属性的对象签名;请改用 。旧的对象语法在编译时静默通过,但现在会导致运行时错误
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
defineStore('storeName', { ... })重大变更: PiniaStorePlugin 类型 — 在 v3.0.0 中移除;请改用 PiniaPlugin。使用 PiniaStorePlugin 的代码将无法编译 来源
重大变更: 在 v3.0.0 中放弃了对 Vue 2 的支持 — Pinia v3 仅支持 Vue 3。Vue 2 用户必须停留在 Pinia v2 来源
重大变更: 在 v3.0.0 中要求 TypeScript 5 或更高版本 — 使用了 TS 4.5 引入的原生 Awaited 类型;推荐使用 TS 5+ 来源
重大变更: 在 v3.0.0 中,IIFE 捆绑包不再捆绑 Vue Devtools — devtools API 体积过大;必须根据你的工作流程手动引入 来源
重大变更: 在 v3.0.0 中,包现在以 type: module 发布 — 仍提供 CJS 分发文件,但包的根目录是 ESM。可能会破坏依赖隐式 CJS 解析的设置 来源
新增: 在 setup 存储中的 action(fn, name?) 辅助函数 — 在 v2.2.0 中添加,可通过 SetupStoreHelpers 参数使用。包装一个函数,使其在存储内被调用时能被 $onAction 追踪;适用于像 Pinia Colada 这样的高级用例 来源
新增: disposePinia(pinia) — 在 v2.1.7 中添加,停止 pinia 效果作用域并移除状态、插件和存储。适用于测试或多 pinia 应用;已处理的实例无法重用 来源
新增: SetupStoreDefinition<Id, SS> 类型 — 在 v2.1.7 中添加,用于 defineStore() 在使用 setup 函数时的返回类型。扩展了 StoreDefinition 并能为 setup 存储提供更好的 IDE 支持 来源
新增: mapWritableState 现在能识别 setup 存储中的可写 computed — 在 v2.3.0 中添加。之前只映射 ref 状态;现在包含从 setup 存储返回的 WritableComputedRef 来源
其他变更: mapGetters 已弃用(mapState 的别名,仍导出) · getActivePinia() 返回 Pinia | undefined(自 v2.0.35 起类型检查更严格) · skipHydrate(obj) 稳定 — 跳过对从 setup 存储返回的非状态对象的 SSR 水合 · shouldHydrate(obj) 导出的实用程序,供插件作者使用
在变更数组或执行多个相关更改时,使用带有函数回调的 $patch() 而不是对象 — 函数形式将所有变更分组到单个 devtools 条目中,并避免创建中间集合 来源
在存储状态上使用 $subscribe() 而不是 watch() — 订阅在每次 $patch 调用时仅触发一次,无论有多少个单独属性被更改,从而避免在使用 $patch 的函数形式时产生冗余回调 来源
当你需要监听器在组件卸载后继续存在时,向 $subscribe() 传递 { detached: true },并向 $onAction() 的第二个参数传递 true — 默认情况下,两者都会在组件卸载时自动移除 来源
在 setup 存储中,使用 skipHydrate() 包装那些不能从 SSR 初始状态获取的状态属性(例如,由 localStorage 支持的可组合项、仅客户端的 ref) — 没有它,服务器序列化的值将覆盖预期的客户端来源 来源
return { lastColor: skipHydrate(lastColor), // 不会被 SSR 状态覆盖 open, }
当插件添加新的状态属性时,在 store.$state(用于 SSR 序列化和 devtools)和 store 上(通过 toRef(store.$state, 'key'))都设置值 — 只设置一个会破坏 devtools 显示或响应性共享 来源
在插件中分配非响应式的外部对象(路由器、类实例、第三方库实例)之前,用 markRaw() 包装它们 — 防止 Vue 尝试深度观察那些本不应是响应式的对象 来源
在组合相互引用的存储时,将 useOtherStore() 调用放在异步操作中的任何 await 之前 — 在 await 之后,活动的 pinia 上下文可能已更改,导致在 SSR 中返回错误的实例 来源
在组件测试中使用来自 @pinia/testing 的 createTestingPinia() 而不是 createPinia() — 它默认会存根所有操作并使其可作为间谍检查;通过 plugins 选项传递插件,而不是通过 testingPinia.use() 来源
在选项存储的 getter 中,优先使用接收 state 作为第一个参数的箭头函数,而不是使用 this 的常规函数 — 箭头函数的返回类型会自动推断,而基于 this 的 getter 需要显式的返回类型注解 来源
每周安装量
108
代码仓库
GitHub 星标数
145
首次出现
2026年2月19日
安全审计
安装于
github-copilot76
codex74
opencode71
gemini-cli70
claude-code69
amp69
piniaIntuitive, type safe and flexible Store for Vue
Version: 3.0.4 Deps: @vue/devtools-api@^7.7.7 Tags: next: 2.0.0-rc.10, beta: 2.1.8-beta.0, latest: 3.0.4
References: Docs — API reference, guides • GitHub Issues — bugs, workarounds, edge cases • GitHub Discussions — Q&A, patterns, recipes • Releases — changelog, breaking changes, new APIs
This section documents version-specific API changes — prioritize recent major/minor releases.
BREAKING: defineStore({ id: 'storeName', ... }) — object signature with id property removed in v3.0.0; use defineStore('storeName', { ... }) instead. Old object syntax silently compiled but is now a runtime error source
BREAKING: PiniaStorePlugin type — removed in v3.0.0; use PiniaPlugin instead. Code using PiniaStorePlugin will fail to compile source
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 native Awaited type introduced in TS 4.5; TS 5+ recommended source
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 as type: module in v3.0.0 — CJS dist files still provided but the package root is ESM. May break setups relying on implicit CJS resolution source
NEW: action(fn, name?) helper in setup stores — added in v2.2.0, available via SetupStoreHelpers parameter. Wraps a function so it is tracked by $onAction when called within the store; intended for advanced use cases like Pinia Colada source
NEW: disposePinia(pinia) — 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
NEW: SetupStoreDefinition<Id, SS> type — added in v2.1.7 for the return type of defineStore() when using a setup function. Extends StoreDefinition and enables better IDE support for setup stores source
NEW: mapWritableState now picks up writable computeds in setup stores — added in v2.3.0. Previously only ref state was mapped; WritableComputedRef returns from setup stores are now included source
Also changed: mapGetters DEPRECATED (alias for mapState, still exported) · getActivePinia() returns Pinia | undefined (typed more strictly since v2.0.35) · skipHydrate(obj) stable — skips SSR hydration for non-state objects returned from setup stores · shouldHydrate(obj) exported utility for plugin authors
Use $patch() with 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
Use $subscribe() instead of watch() on store state — subscriptions fire only once per $patch call regardless of how many individual properties changed, avoiding redundant callbacks when using the function form of $patch source
Pass { detached: true } to $subscribe() and true as the second arg to when you need listeners to outlive the component — by default both are automatically removed on component unmount
Weekly Installs
108
Repository
GitHub Stars
145
First Seen
Feb 19, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
github-copilot76
codex74
opencode71
gemini-cli70
claude-code69
amp69
Flutter应用架构设计指南:分层结构、数据层实现与最佳实践
4,500 周安装
$onAction()In setup stores, use skipHydrate() to wrap state properties that must not be picked up from SSR initial state (e.g., composables backed by localStorage, client-only refs) — without it, the server's serialized value will override the intended client-side source source
return { lastColor: skipHydrate(lastColor), // won't be overwritten by SSR state open, }
When a plugin adds new state properties, set the value on both store.$state (for SSR serialization and devtools) and store via toRef(store.$state, 'key') — setting only one breaks devtools display or reactivity sharing source
Wrap non-reactive external objects (router, class instances, third-party lib instances) with markRaw() before assigning them in plugins — prevents Vue from trying to deeply observe objects that aren't meant to be reactive source
When composing stores that reference each other, place useOtherStore() calls before any await in async actions — after an await the active pinia context may have changed, causing the wrong instance to be returned in SSR source
Use createTestingPinia() from @pinia/testing for component tests rather than createPinia() — it stubs all actions by default and makes them inspectable as spies; pass plugins via the plugins option, not via testingPinia.use() source
In options store getters, prefer arrow functions that receive state as the first parameter over regular functions using this — arrow function return types are inferred automatically, while this-based getters require an explicit return type annotation source