全局状态管理规范 (Pinia)
Development全局状态管理规范 (Pinia) is an Agent Skill for development published in the TencentBlueKing/bk-bcs repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.
基于 Pinia 的全局状态管理规范,包含 UserStore、AppStore 的标准定义
QUICK START
How to use this skill
Bring this guide into your coding agent with a prompt tailored to the tool you use.
- Open your project in Codex.
- Copy and paste the prompt below.
- Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/TencentBlueKing/bk-bcs/blob/HEAD/bcs-services/bcs-project-manager/.cursor/skills/pinia-setup/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/全局状态管理规范-pinia, but wait for my approval before writing files or running scripts.
Copying this prompt does not install or run the skill. Review third-party files before use.
全局状态管理规范 (Pinia)
推荐使用 Setup Store 语法(类似 Composition API),比 Options API 更灵活。
基础模式
// 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 };
});
组件中使用
<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 规则判断是否需要加载