This skill provides agents with development and review guidelines for Vue 3 (Composition API) and Pinia state management. It helps produce code that conforms to Vue Style Guide priority A/B rules, avoid common reactivity pitfalls, and generate consistent, strongly typed Single-File Components (SFC).
Gotchas
- Destructuring reactive objects causes loss of reactivity: Directly destructuring
propsor objects defined byreactive()is prohibited (for example:const { name } = propsorconst { x } = state), as this completely loses Vue reactivity tracking!- Error prevention mechanism: You must use
toRefs(props)ortoRef(props, 'name')to preserve its reactivity.
- Error prevention mechanism: You must use
- Destructuring a Pinia Store causes loss of state reactivity: Directly destructuring a Pinia store's state (such as
const { user } = useAuthStore()) causes the state to lose two-way reactivity!- Error prevention mechanism: You must use
storeToRefs(store)to destructure state; actions, however, can be destructured directly and normally.
- Error prevention mechanism: You must use
- Manual and cumbersome v-model binding: Before Vue 3.4, custom two-way binding required manually declaring the
modelValueprop and emitting theupdate:modelValueevent, which is prone to mistakes.- Error prevention mechanism: In Vue 3.4+ projects, preferentially use the
defineModel()macro to declare two-way binding, reducing the risk of inconsistency between props and events.
- Error prevention mechanism: In Vue 3.4+ projects, preferentially use the
- Mixing v-if and v-for on the same node: In Vue 3,
v-ifhas higher priority thanv-for, which means thev-ifconditional checkcannotaccess variables in thev-forloop, and will directly cause compile-time or runtime errors!- Error prevention mechanism: Always use the
<template>tag to wrapv-for, then usev-ifinside the child node, or use acomputedcomputed property in advance to filter the array.
- Error prevention mechanism: Always use the
Workflow Checklist
Progress:
- Step 1: Perceive Setup & Tooling
- Step 2: Load Guides & Best Practices
- Step 3: Reasoning & Architecture
- Step 4: Generate SFC & Stores
- Step 5: Code Quality Review
Detailed Guidelines
Step 1 — Perceive Setup & Tooling (Perceive)
- Check dependency configuration: Read
package.json, determine the Vue version (confirm it is Vue 3.x) and whetherpinia、vue-router。 - Identify the build tool and language: Confirm whether it is a Vite project, and whether TypeScript (
lang="ts") and Scoped CSS are enabled.
Step 2 — Load Coding Standards & Pitfall Guide (Reason)
Before performing Vue 3 development or review,the deep knowledge base must be dynamically loaded:
- Read
references/coding-style.mdto confirm official priority conventions such as component naming (must be multi-word PascalCase), prop definitions, etc. - Read
references/anti-patterns.mdto avoid anti-patterns such as props mutation and computed side effects. - Read
references/patterns.mdto achieve reasonable splitting of Composables and architectural design of Pinia Setup Stores.
Step 3 — Architectural Reasoning and Reactivity Planning (Reason)
Before writing code, first consider the following design:
- Reactivity choice: Unless it is a complex object where multiple properties need to be highly interrelated, otherwiseprefer using
ref()instead ofreactive(), to ensure consistency when accessing.valueand avoid destructuring deactivation. - State sinking: Evaluate whether the state belongs to component-local state (use
ref) or global state (usePinia Setup Store)。
Step 4 — Apply templates to generate consistent code (Act)
- SFC structure: Newly created components must use the
<script setup>syntax, and according to project settings prioritize addinglang="ts"。 - Apply templates: Read and refer to
assets/Vue3SFCComponentTemplate.vueas a layout example for a standard Vue 3 component. - Mandatory language and style: All code comments and explanations must use Traditional Chinese (Taiwan). Terminology must be aligned (for example: component, property, state management, reactivity, two-way binding, computed property).
Step 5 — Code health check and self-proofreading (Act)
- Confirm that no Side Effects such as state modification or API calls are performed in
computed. - Confirm that no direct assignment or modification is made to
props. - Confirm that the
v-forbinding ofkeyuses a stable unique ID, rather than the array indexindex。
Output Templates
When generating a new component, output according to the following consistent structure; for more implementation details, please refer to assets/Vue3SFCComponentTemplate.vue:
<template>
<div class="vue-expert-component">
<!-- 模板內容 -->
</div>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue';
// 1. 定義屬性與事件 (Props, Emits, Models)
const props = defineProps<{
title: string;
}>();
const emit = defineEmits<{
(e: 'change', value: string): void;
}>();
// Vue 3.4+ 雙向綁定巨集
const modelValue = defineModel<string>();
// 2. 狀態定義 (State)
const count = ref(0);
// 3. 計算屬性 (Computed)
const doubleCount = computed(() => count.value * 2);
// 4. 方法定義 (Methods)
function handleAction() {
emit('change', 'action-triggered');
}
</script>
<style scoped>
/* 必須使用 scoped 以免全域樣式污染,並優先選用 class 選擇器 */
.vue-expert-component {
display: flex;
flex-direction: column;
}
</style>