pinia
它能做什么
这项技能说明如何使用 Pinia 组织 Vue 应用中的共享数据。Pinia 是 Vue 官方的状态管理库。适合需要在应用多个部分使用同一份数据的开发者。
适用场景
- ✓当你在构建 Vue 应用,并需要在许多组件之间共享数据时。
- ✓当你想设置带有状态、获取器和动作的仓库时。
- ✓当你需要测试或扩展 Pinia 仓库的时候。
技能链接
https://github.com/antfu/skills/blob/main/skills/pinia/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请创建一个 Pinia 仓库,在所有页面中保存我的购物车商品和总价。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体设置用于保存共享数据的仓库,以及修改这些数据的动作,并遵循 Pinia 关于类型、测试和扩展的指导。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
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 v4.0.3, generated at 2026-09-25.
Core References
| Topic | Description | Reference |
|---|---|---|
| Stores | Defining stores, state, getters, actions, storeToRefs, subscriptions | core-stores |
Features
Extensibility
| Topic | Description | Reference |
|---|---|---|
| Plugins | Extend stores with custom properties, state, and behavior | features-plugins |
Composability
| Topic | Description | Reference |
|---|---|---|
| Composables | Using Vue composables within stores (VueUse, etc.) | features-composables |
| Composing Stores | Store-to-store communication, avoiding circular dependencies | features-composing-stores |
Best Practices
| Topic | Description | Reference |
|---|---|---|
| Testing | Unit testing with @pinia/testing, mocking, stubbing | best-practices-testing |
| Outside Components | Using stores in navigation guards, plugins, middlewares | best-practices-outside-component |
Advanced
| Topic | Description | Reference |
|---|---|---|
| SSR | Server-side rendering, state hydration | advanced-ssr |
| Nuxt | Nuxt integration, auto-imports, SSR best practices | advanced-nuxt |
| HMR | Hot module replacement for development | 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/testingfor component tests with mocked stores
常见问题
Pinia 只能用于 Vue 吗?
它是为 Vue 应用构建的,这也正是该指导所涵盖的内容。
我可以测试我的仓库吗?
可以。指导涵盖测试仓库和模拟仓库。
它支持 TypeScript 吗?
支持。Pinia 设计为类型安全,并支持 TypeScript。
详情
作者的原始描述
Pinia official Vue state management library, type-safe and extensible. Use when defining stores, working with state/getters/actions, or implementing store patterns in Vue apps.
repo: antfu/skills
热度
- GitHub 星标
- 6.0k
- 排名
- #14442
- 许可证
- MIT
- 更新时间
- 2026/10/30
评论
- …