Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Vue.js 3 Composition API patterns, component architecture, reactivity best practices, Pinia state management, Vue Router navigation, and Nuxt SSR patterns. Activates for Vue, Nuxt, Vite, or Pinia projects.
.claude/skills/affaan-m-vue-patterns/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 117% | 0% |
| case-20 | ✓→✗ | ▼ Worse | 331% | 0% |
| case-06 | ✓→✓ | = Same ✓ | 191% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 205% | 0% |
| case-03 | ✓→✓ | = Same ✓ | 202% | 0% |
Comprehensive guide for Vue.js 3 development using Composition API (<script setup>), covering component design, reactivity, state management, routing, testing, and SSR patterns. Nuxt-specific guidance is included where it differs from vanilla Vue.
Activate this skill when:
.vue files).src/
├── api/ # API client and endpoint definitions
├── assets/ # Static assets (images, fonts, icons)
├── components/ # Shared/reusable components
│ ├── base/ # Base UI primitives (Button, Input, Modal)
│ └── features/ # Feature-specific shared components
├── composables/ # Reusable Composition API logic
├── layouts/ # Page layouts (optional)
├── pages/ # Route-level page components
├── router/ # Vue Router configuration
├── stores/ # Pinia stores
├── types/ # TypeScript type definitions
├── utils/ # Pure utility functions
└── App.vue # Root component| Convention | When to Use | |-----------|-------------| | PascalCase.vue | All components (enforced by vue/multi-word-component-names) | | useCamelCase.ts | Composables | | camelCase.ts | Utilities, API clients, types | | kebab-case directories | Route segments, feature folders |
vue<script setup lang="ts"> // 1. Imports (vue → ecosystem → absolute → relative) // 2. Props & Emits & Slots // 3. Composables // 4. Local state (ref/reactive) // 5. Computed properties // 6. Methods // 7. Watchers // 8. Lifecycle hooks </script> <template> <!-- Template content --> </template> <style scoped> /* Scoped styles */ </style>
ts// Type-based props with defaults interface Props { label: string; variant?: "primary" | "secondary"; disabled?: boolean; items: Item[]; } const props = withDefaults(defineProps<Props>(), { variant: "primary", disabled: false, });
type, and required/default where appropriate.isXxx, hasXxx, canXxx.defineModel() (Vue 3.4+) or modelValue + update:modelValue.tsconst emit = defineEmits<{ submit: []; "update:modelValue": [value: string]; select: [id: string, index: number]; }>();
@update:model-value).emit("update:modelValue", val)).ts// composables/useDebounce.ts export function useDebounce<T>(value: MaybeRef<T>, delay: number): Ref<T> { const debounced = ref(toValue(value)) as Ref<T>; let timer: ReturnType<typeof setTimeout>; watch( () => toValue(value), (newVal) => { clearTimeout(timer); timer = setTimeout(() => { debounced.value = newVal; }, delay); } ); onUnmounted(() => clearTimeout(timer)); return readonly(debounced); }
use prefix.ref, computed, reactive), never plain primitives.MaybeRef / toRef() / toValue().onUnmounted or watcher onCleanup.Composables replace Vue 2 mixins entirely:
| Pattern | Use Case | |---------|----------| | ref() / reactive() | Local component state | | Props + Emits | Parent-child communication | | Provide / Inject | Theme, config, plugin API | | Pinia store | Global, shared, complex state | | Server state composable | API data with caching (wrap fetch/TanStack Query) |
ts// stores/useCartStore.ts export const useCartStore = defineStore("cart", () => { const items = ref<CartItem[]>([]); const isLoading = ref(false); const totalPrice = computed(() => items.value.reduce((sum, i) => sum + i.price * i.quantity, 0) ); const itemCount = computed(() => items.value.reduce((sum, i) => sum + i.quantity, 0) ); async function addItem(productId: string) { isLoading.value = true; try { const item = await fetchProduct(productId); const existing = items.value.find(i => i.id === item.id); if (existing) existing.quantity++; else items.value.push({ ...item, quantity: 1 }); } finally { isLoading.value = false; } } return { items, isLoading, totalPrice, itemCount, addItem }; });
$patch() for grouped updates.tsconst routes = [ { path: "/users/:id", name: "user-detail", component: () => import("@/pages/UserDetail.vue"), // lazy props: true, // pass params as props meta: { requiresAuth: true }, }, ];
tsrouter.beforeEach((to, from) => { const { isLoggedIn } = useAuthStore(); if (to.meta.requiresAuth && !isLoggedIn) { return { name: "login", query: { redirect: to.fullPath } }; } });
When a component stays mounted but route params change:
tsconst route = useRoute(); const id = computed(() => route.params.id as string); watch(id, (newId) => fetchItem(newId));
vue<!-- v-if/v-else-if/v-else --> <div v-if="isLoading">Loading...</div> <div v-else-if="error">Error: {{ error }}</div> <div v-else>{{ content }}</div> <!-- v-show for frequent toggles --> <div v-show="isOpen">Toggled content</div> <!-- v-for with stable keys --> <div v-for="item in items" :key="item.id">{{ item.name }}</div> <!-- Computed filtered list (not v-if + v-for on same element) --> <div v-for="item in activeItems" :key="item.id">{{ item.name }}</div> <!-- Event handling --> <form @submit.prevent="handleSubmit"> <button type="submit">Save</button> </form> <!-- v-model --> <input v-model="name" /> <CustomInput v-model="value" v-model:title="title" />
| Technique | When to Use | |-----------|-------------| | v-memo | List items that rarely change | | v-once | Content rendered once and static forever | | shallowRef() | Large data structures replaced wholesale | | shallowReactive() | Only top-level properties are reactive | | v-show over v-if | Frequent visibility toggles | | <KeepAlive :max="10"> | Cache toggled views | | Lazy routes | () => import(...) for non-critical routes | | Suspense | Async component loading with fallback |
tsimport { mount } from "@vue/test-utils"; import { createPinia, setActivePinia } from "pinia"; import UserCard from "./UserCard.vue"; beforeEach(() => { setActivePinia(createPinia()); }); it("renders and emits", async () => { const wrapper = mount(UserCard, { props: { user: { id: "1", name: "Alice" } }, }); expect(wrapper.text()).toContain("Alice"); await wrapper.find("button").trigger("click"); expect(wrapper.emitted("select")![0]).toEqual(["1"]); });
Nuxt auto-imports ref, computed, watch, useFetch, useAsyncData, etc. Use them directly without importing. For non-Nuxt projects, always import explicitly.
tsconst { data: user, pending, error, refresh } = await useAsyncData( "user", // unique key for caching () => $fetch(`/api/users/${id}`), ); const { data: posts } = await useFetch("/api/posts", { query: { page: 1 }, key: "posts-page-1", // dedupes requests });
ts// server/api/users/[id].ts export default defineEventHandler(async (event) => { const { id } = await getValidatedRouterParams(event, z.object({ id: z.string().uuid(), }).parse); // ... fetch and return });
ts// nuxt.config.ts export default defineNuxtConfig({ runtimeConfig: { // server-only apiSecret: "", // public (exposed to client) public: { apiBase: "https://api.example.com", }, }, });
Vue 3.5 stabilized reactive props destructure — destructured variables from defineProps() are automatically reactive:
ts// Vue 3.5+: destructured props are reactive (no need for toRefs) const { count = 0, msg = "hello" } = defineProps<{ count?: number; msg?: string; }>(); // Limitation: cannot watch destructured prop directly watch(() => count, (newVal) => { ... }); // PASS getter required
useTemplateRef()Replace name-matched plain refs with useTemplateRef() for template references:
tsimport { useTemplateRef } from "vue"; const inputEl = useTemplateRef<HTMLInputElement>("input"); // "input" matches the ref="input" attribute in template, not the variable name
Supports dynamic ref IDs: useTemplateRef(dynamicRefId).
onWatcherCleanup()Globally importable watcher cleanup API (Vue 3.5+). It must be called synchronously inside the watcher callback:
tsimport { watch, onWatcherCleanup } from "vue"; watch(userId, async (newId) => { const controller = new AbortController(); onWatcherCleanup(() => controller.abort()); // ... fetch with signal });
useId()SSR-stable unique ID generation for form elements and accessibility:
tsimport { useId } from "vue"; const id = useId();
defer Teleport<Teleport defer> allows teleporting to targets rendered in the same cycle:
vue<Teleport defer to="#container">Content</Teleport> <div id="container"></div>
defineAsyncComponent() now supports hydrate strategy:
tsimport { defineAsyncComponent, hydrateOnVisible } from "vue"; const AsyncComp = defineAsyncComponent({ loader: () => import("./Comp.vue"), hydrate: hydrateOnVisible(), });
| Anti-Pattern | Why It's Wrong | The Fix | |-------------|---------------|---------| | Destructuring defineProps() (Vue < 3.5) | Captures snapshot, loses reactivity | Access via props.xxx or use toRefs() | | watch() on destructured prop (Vue 3.5+) | Compile-time error — destructured props can't be watched directly | Use getter wrapper: watch(() => count, ...) | | v-if + v-for on same element | Ambiguous execution order | Use computed filtered array | | v-for key = index | Broken state on reorder | Use stable database IDs | | Mutating props | Violates one-way data flow | Emit events or use v-model | | v-html with user content | XSS vulnerability | Sanitize with DOMPurify | | Mixins in Vue 3 | Opaque, collision-prone | Replace with composables | | Module-scope side effects in composable | Shared across instances | Scope in onMounted + onUnmounted | | reactive() for replaceable state | Replacement breaks reactivity | Use ref() instead | | Watcher without cleanup | Memory leaks, race conditions | Use onCleanup or onWatcherCleanup() (Vue 3.5+) | | Options API in new Vue 3 code | Ecosystem move to Composition API | Use <script setup> | | Plain ref for template references | No dynamic ref support, name-matching fragile | Use useTemplateRef() (Vue 3.5+) |
accessibility — ARIA, semantic HTML, focus managementfrontend-patterns — Cross-framework frontend architecturetypescript — TypeScript best practices applied to Vue projectscoding-standards — General code quality standards| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-06 | pass→pass | 10,456 | 11,716 | +12% | 1 | 1 | 0% | 2,050 | 5,974 | +191% | 0 | 0 | — |
case-01 | fail→pass | 17,013 | 18,394 | +8% | 1 | 1 | 0% | 3,641 | 7,911 | +117% | 0 | 0 | — |
case-02 | pass→pass | 10,788 | 16,259 | +51% | 1 | 1 | 0% | 2,441 | 7,454 | +205% | 0 | 0 | — |
case-03 | pass→pass | 7,984 | 4,782 | -40% | 1 | 1 | 0% | 1,462 | 4,422 | +202% | 0 | 0 | — |
case-04 | pass→pass | 7,873 | 5,566 | -29% | 1 | 1 | 0% | 1,406 | 4,575 | +225% | 0 | 0 | — |
case-05 | pass→pass | 7,755 | 7,220 | -7% | 1 | 1 | 0% | 1,474 | 4,974 | +237% | 0 | 0 | — |
case-07 | pass→pass | 12,671 | 13,397 | +6% | 1 | 1 | 0% | 2,609 | 6,390 | +145% | 0 | 0 | — |
case-08 | pass→pass | 12,284 | 13,642 | +11% | 1 | 1 | 0% | 2,688 | 6,652 | +147% | 0 | 0 | — |
case-09 | pass→pass | 6,556 | 5,776 | -12% | 1 | 1 | 0% | 1,308 | 4,709 | +260% | 0 | 0 | — |
case-10 | pass→pass | 25,680 | 6,425 | -75% | 1 | 1 | 0% | 1,549 | 4,781 | +209% | 0 | 0 | — |
case-11 | pass→pass | 11,157 | 11,225 | +1% | 1 | 1 | 0% | 2,248 | 5,798 | +158% | 0 | 0 | — |
case-12 | pass→pass | 12,197 | 10,828 | -11% | 1 | 1 | 0% | 2,108 | 5,484 | +160% | 0 | 0 | — |
case-13 | fail→fail | 12,490 | 9,293 | -26% | 1 | 1 | 0% | 2,751 | 5,578 | +103% | 0 | 0 | — |
case-14 | pass→pass | 10,490 | 7,047 | -33% | 1 | 1 | 0% | 1,472 | 5,067 | +244% | 0 | 0 | — |
case-15 | pass→pass | 9,839 | 8,249 | -16% | 1 | 1 | 0% | 1,868 | 4,994 | +167% | 0 | 0 | — |
case-16 | pass→pass | 4,357 | 5,105 | +17% | 1 | 1 | 0% | 941 | 4,599 | +389% | 0 | 0 | — |
case-17 | pass→pass | 18,100 | 5,412 | -70% | 1 | 1 | 0% | 1,599 | 4,556 | +185% | 0 | 0 | — |
case-18 | pass→pass | 13,353 | 10,169 | -24% | 1 | 1 | 0% | 2,536 | 5,543 | +119% | 0 | 0 | — |
case-19 | pass→pass | 7,130 | 6,209 | -13% | 1 | 1 | 0% | 1,311 | 4,792 | +266% | 0 | 0 | — |
case-20 | pass→fail | 5,660 | 5,793 | +2% | 1 | 1 | 0% | 1,110 | 4,787 | +331% | 0 | 0 | — |
case-21 | pass→pass | 10,275 | 7,302 | -29% | 1 | 1 | 0% | 2,113 | 5,079 | +140% | 0 | 0 | — |
case-22 | pass→pass | 16,360 | 14,174 | -13% | 1 | 1 | 0% | 3,899 | 6,901 | +77% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of 0 percentage points is the difference between those two pass rates over the 22 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.