dist/plugins/web-framework-vue-composition-api/skills/web-framework-vue-composition-api/SKILL.md
Vue 3 Composition API patterns, reactivity primitives, composables, lifecycle hooks
npx skillsauth add agents-inc/skills web-framework-vue-composition-apiInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Quick Guide: Use
<script setup>for all components.ref()for primitives,reactive()for objects. Extract reusable logic into composables (use*functions). Clean up side effects inonUnmounted. UsedefineModel()for v-model (3.4+),useTemplateRef()for DOM refs (3.5+),onWatcherCleanup()to cancel stale async work (3.5+). Destructured props require getter wrappers inwatch().
<critical_requirements>
All code must follow project conventions in CLAUDE.md (kebab-case, named exports, import ordering,
import type, named constants)
(You MUST use <script setup> syntax for all new Vue components)
(You MUST clean up all side effects (timers, listeners, subscriptions) in onUnmounted)
(You MUST use ref() for primitives and reactive() for objects - access ref values via .value)
(You MUST prefix all composable functions with use following Vue conventions)
(You MUST wrap destructured props in a getter for watch() - watch(() => count, ...) not watch(count, ...))
</critical_requirements>
Auto-detection: Vue 3 Composition API, script setup, ref, reactive, computed, watch, watchEffect, composables, onMounted, onUnmounted, defineProps, defineEmits, defineExpose, defineModel, useTemplateRef, useId, onWatcherCleanup, provide, inject, Suspense
When to use:
Key patterns covered:
When NOT to use:
The Composition API enables organizing code by logical concern rather than by option type (data, methods, computed). This makes complex components more maintainable and enables powerful logic reuse through composables.
Core principles:
ref() and reactive()All variables/functions in <script setup> are automatically available in the template. Use TypeScript generics with defineProps and defineEmits for type-safe interfaces.
<script setup lang="ts">
import { ref, computed } from "vue";
const props = defineProps<{
userId: string;
initialCount?: number;
}>();
const emit = defineEmits<{
update: [value: number];
submit: [];
}>();
const count = ref(props.initialCount ?? 0);
const doubleCount = computed(() => count.value * 2);
function increment() {
count.value++;
emit("update", count.value);
}
</script>
Why good: No explicit return needed, TypeScript types flow naturally, named tuple emit syntax (Vue 3.3+) self-documents payloads
See examples/core.md for a complete component with loading/error handling.
ref() for primitives and reassignable values, reactive() for objects with nested properties. Access ref values via .value in script; templates unwrap automatically.
const count = ref(0); // Primitive -> ref
count.value++; // .value in script
const state = reactive({
// Nested object -> reactive
user: null as User | null,
settings: { theme: "light" },
});
state.settings.theme = "dark"; // Direct access, no .value
Gotcha: Destructuring reactive() loses reactivity - use toRefs(state) if you need to destructure.
See examples/reactivity.md for ref/reactive/computed patterns and anti-patterns.
Skip if using Nuxt — use useFetch or useAsyncData instead.
watch() for explicit sources with access to old values. watchEffect() for automatic dependency tracking that runs immediately. Use onWatcherCleanup() (Vue 3.5+) to cancel stale async work.
// watch: explicit source, access to old value
watch(searchQuery, async (newQuery, oldQuery) => {
/* ... */
});
// watchEffect: auto-tracks dependencies, runs immediately
watchEffect(async () => {
if (userId.value) userData.value = await fetchUser(userId.value);
});
// Cleanup: cancel stale requests (Vue 3.5+)
watch(searchQuery, async (query) => {
const controller = new AbortController();
onWatcherCleanup(() => controller.abort());
const res = await fetch(`/api/search?q=${query}`, {
signal: controller.signal,
});
});
Gotcha: Watch reactive object properties with a getter: watch(() => state.count, ...) not watch(state.count, ...).
See examples/vue-3-5-features.md for complete onWatcherCleanup patterns.
Always pair onMounted setup with onUnmounted cleanup. Timers, listeners, observers, WebSockets - anything opened must be closed.
const POLL_INTERVAL_MS = 5000;
let intervalId: ReturnType<typeof setInterval> | null = null;
onMounted(() => {
intervalId = setInterval(fetchData, POLL_INTERVAL_MS);
});
onUnmounted(() => {
if (intervalId) {
clearInterval(intervalId);
intervalId = null;
}
});
See examples/lifecycle.md for WebSocket reconnection and event listener cleanup patterns.
Extract reusable stateful logic into use* functions. Return objects with refs (not bare values) so destructuring preserves reactivity.
export function useCounter(options: UseCounterOptions = {}) {
const { initialValue = 0, min = -Infinity, max = Infinity } = options;
const count = ref(initialValue);
const isAtMax = computed(() => count.value >= max);
function increment() {
if (count.value < max) count.value++;
}
function reset() {
count.value = initialValue;
}
return { count, isAtMax, increment, reset }; // Return object with refs
}
Async composables should accept MaybeRefOrGetter<T> inputs (use toValue() to normalize) and return { data, error, isLoading } refs.
See examples/composables.md for useFetch, useLocalStorage, useDebounce, and useIntersectionObserver implementations.
Replaces the defineProps + defineEmits boilerplate for two-way binding. Returns a ref-like value that syncs with the parent.
<script setup lang="ts">
const model = defineModel<string>(); // Single v-model
const firstName = defineModel<string>("firstName"); // Named v-model
const [model, modifiers] = defineModel<string>({
// With modifiers
set(value) {
return modifiers.capitalize
? value.charAt(0).toUpperCase() + value.slice(1)
: value;
},
});
</script>
See examples/vue-3-5-features.md for complete defineModel examples with named models and modifiers.
useTemplateRef() separates template refs from reactive refs. Use for dynamic ref names and in composables. Traditional ref() still works for simple static refs.
<script setup lang="ts">
const inputRef = useTemplateRef<HTMLInputElement>("myInput");
onMounted(() => inputRef.value?.focus());
</script>
<template>
<input ref="myInput" type="text" />
</template>
For child component refs: Use defineExpose() to declare the public API, then ref<InstanceType<typeof Child>>() in the parent.
See examples/define-expose.md for form validation with exposed methods and examples/vue-3-5-features.md for useTemplateRef in composables.
Generates SSR-safe unique IDs for form labels and ARIA attributes. Each call produces a different ID. Must be called in setup (not in computed).
<script setup lang="ts">
const id = useId();
</script>
<template>
<label :for="id">Email</label>
<input :id="id" type="email" />
</template>
See examples/vue-3-5-features.md for multi-field forms and ARIA patterns.
Destructured props are automatically reactive. Use JavaScript default syntax instead of withDefaults(). The critical gotcha: destructured props require a getter wrapper in watch().
<script setup lang="ts">
const {
title,
count = 0,
items = () => [],
} = defineProps<{
title: string;
count?: number;
items?: string[];
}>();
// CORRECT: getter wrapper
watch(
() => count,
(newCount) => {
/* ... */
},
);
// WRONG: passes value, not reactive source
// watch(count, ...) // Never triggers!
</script>
See examples/vue-3-5-features.md for complete reactive destructure examples.
Type-safe dependency injection to avoid prop drilling. Define InjectionKey<T> symbols in a separate file, provide in ancestor, inject in descendant with an explicit error for missing providers.
// injection-keys.ts
export const THEME_KEY: InjectionKey<ThemeContext> = Symbol("theme");
// Provider: provide(THEME_KEY, { theme, toggleTheme });
// Consumer: const ctx = inject(THEME_KEY);
// if (!ctx) throw new Error("Must be used within ThemeProvider");
See examples/provide-inject.md for a complete theme provider/consumer pattern.
defineAsyncComponent for code-splitting. Top-level await in <script setup> makes a component async (requires <Suspense> in parent). Use onErrorCaptured at the Suspense boundary for error handling.
const LOADING_DELAY_MS = 200;
const LOAD_TIMEOUT_MS = 10000;
const HeavyChart = defineAsyncComponent({
loader: () => import("@/components/HeavyChart.vue"),
loadingComponent: LoadingSpinner,
delay: LOADING_DELAY_MS,
timeout: LOAD_TIMEOUT_MS,
});
See examples/async.md for Suspense boundaries with error handling.
</patterns>Detailed Resources:
<red_flags>
High Priority Issues:
onUnmounted - timers, listeners, subscriptions, WebSockets cause memory leaksref.value in template - templates auto-unwrap refs, writing .value in templates is wrongreactive() without toRefs() - loses reactivity silentlywatch(count, ...) never triggers, use watch(() => count, ...)Medium Priority Issues:
onWatcherCleanup() (3.5+) or the cleanup callbackprovide() with string keys instead of typed InjectionKey<T> symbols - loses type safetyGotchas & Edge Cases:
watchEffect runs immediately; watch is lazy by defaultawait makes a component async and requires <Suspense> in parentref() or reactive() if consumers need reactivityonUnmounted won't run if component errors during setup - use error boundaries for critical cleanupuseId() must not be called in computed - it generates a new ID each calldefineModel returns a ref - use .value in script, auto-unwrapped in template</red_flags>
<critical_reminders>
All code must follow project conventions in CLAUDE.md
(You MUST use <script setup> syntax for all new Vue components)
(You MUST clean up all side effects (timers, listeners, subscriptions) in onUnmounted)
(You MUST use ref() for primitives and reactive() for objects - access ref values via .value)
(You MUST prefix all composable functions with use following Vue conventions)
(You MUST wrap destructured props in a getter for watch() - watch(() => count, ...) not watch(count, ...))
Failure to follow these rules will cause memory leaks, broken reactivity, and unmaintainable component APIs.
</critical_reminders>
development
Material Design component library for Vue 3
development
VitePress 1.x — Vue-powered static site generator for documentation sites, built on Vite
tools
Docusaurus 3.x documentation framework — site configuration, docs/blog plugins, sidebars, versioning, MDX, swizzling, and deployment
development
TanStack Form patterns - useForm, form.Field, validators, arrays, linked fields, createFormHook, type safety