Skip to content
Back to skills

Pinia Setup

ASecurity

基于 Pinia 的全局状态管理规范,包含 UserStore、AppStore 的标准定义

  • 840 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added February 7, 2026
devopstypescriptgovueapi

Works with

  • api

Security analysis

A100/100

Scanned February 12, 2026

npx -y skills add TencentBlueKing/bk-bcs --skill pinia-setup --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Pinia Setup?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Pinia Setup
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tencentblueking-pinia-setup/badge)](https://www.skillsdirectory.com/skills/tencentblueking-pinia-setup)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: eng-state-management
name: 全局状态管理规范 (Pinia)
category: engineering
description: 基于 Pinia 的全局状态管理规范,包含 UserStore、AppStore 的标准定义
tags: [pinia, store, state, vue3]
updated_at: 2026-01-09
---

# 全局状态管理规范 (Pinia)

推荐使用 **Setup Store** 语法(类似 Composition API),比 Options API 更灵活。

## 基础模式

```typescript
// src/store/user.ts
import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useUserStore = defineStore('user', () => {
  const userInfo = ref(null);
  const loading = ref(false);

  const fetchUserInfo = async () => {
    if (userInfo.value) return userInfo.value;
    loading.value = true;
    try {
      userInfo.value = await http.get('/user/info');
    } finally {
      loading.value = false;
    }
  };

  return { userInfo, loading, fetchUserInfo };
});
```

## 组件中使用

```typescript
<script setup lang="ts">
import { useUserStore } from '@/store/user';
import { storeToRefs } from 'pinia';

const userStore = useUserStore();
// 使用 storeToRefs 保持响应性
const { userInfo, loading } = storeToRefs(userStore);
// Action 直接调用
userStore.fetchUserInfo();
</script>
```

## 常见错误

| 错误 | 解决 |
|------|------|
| 解构丢失响应性 | 用 `storeToRefs()` |
| 多次实例化 | Store 单例,直接 `useXxxStore()` |

## 📦 按需加载资源

| 资源 | URI |
|-----|-----|
| 完整 Store 模板 | `skill://pinia-setup/assets/store-template.ts` |


---
## 📦 可用资源

- `skill://pinia-setup/assets/store-template.ts`

> 根据 SKILL.md 中的 IF-THEN 规则判断是否需要加载

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…