Index of every page on this site: llms.txt
All skills
ejirocodes avatar

/vue-best-practices

@6e805e4
Run it yourself

Vue 3 and Vue.js best practices for TypeScript, vue-tsc, Volar, and component patterns. Use when writing, reviewing, or refactoring Vue 3 components with TypeScript, configuring Volar/vueCompilerOptions, extracting component types, working with defineModel/withDefaults, setting up Pinia store tests, or debugging Vue tooling issues. Triggers on Vue components, props extraction, wrapper components, template type checking, strictTemplates, vueCompilerOptions, Volar 3, CSS modules, fallthrough attributes, defineModel, withDefaults, deep watch, vue-router typed params, Pinia mocking, HMR SSR, moduleResolution bundler, useTemplateRef, onWatcherCleanup, useId, generic components, reactive props destructure.

Use this Skill: https://skilld.dev/gh/ejirocodes/agent-skills/vue-best-practices

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencescomponents.md

≈2.2k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Component Patterns

Table of Contents


defineModel Patterns

Components using defineModel may fire the @update:model-value event with undefined in certain edge cases. TypeScript types don't always reflect this behavior.

Version Note: This issue may be resolved in Vue 3.5+. Testing with Vue 3.5.26 could not reproduce the double emission with undefined. Verify the issue exists in your specific scenario before applying workarounds.

Symptoms

  • Parent component receives undefined unexpectedly
  • Runtime error: "Cannot read property of undefined"
  • Type mismatch between expected T and received T | undefined
  • Issue appears when clearing/resetting the model value

Root Cause

defineModel returns Ref<T | undefined> by default, even when T is non-nullable. The update event can fire with undefined when:

  • Component unmounts
  • Model is explicitly cleared
  • Internal state resets

Fix

Option 1: Use required option (Vue 3.5+) - Recommended

// Returns Ref<Item> instead of Ref<Item | undefined>
const model = defineModel<Item>({ required: true })

Option 2: Type parent handler to accept undefined

<template>
  <MyComponent
    v-model="item"
    @update:model-value="handleUpdate"
  />
</template>

<script setup lang="ts">
// Handle both value and undefined
const handleUpdate = (value: Item | undefined) => {
  if (value !== undefined) {
    item.value = value
  }
}
</script>

Option 3: Use default value in defineModel

const model = defineModel<string>({ default: '' })

Multiple v-models

<script setup lang="ts">
// Named models
const firstName = defineModel<string>('firstName', { required: true })
const lastName = defineModel<string>('lastName', { required: true })

// Default model
const selected = defineModel<boolean>({ default: false })
</script>

Type Declaration Pattern

// In child component
interface Props {
  modelValue: Item
}
const model = defineModel<Item>({ required: true })

// Emits will be typed as (value: Item) not (value: Item | undefined)

Reference: vuejs/core#12817


Deep Watch with Numeric Depth

Vue 3.5 introduced deep: number for watch depth control. This allows watching array mutations without the performance cost of deep traversal.

Symptoms

  • Array mutations not triggering watch callback
  • Deep watch causing performance issues on large nested objects

The Feature

// Vue 3.5+ only
watch(items, (newVal) => {
  // Triggered on array mutations (push, pop, splice, etc.)
}, { deep: 1 })
deep value Behavior
true Full recursive traversal (original behavior)
false Only reference changes
1 One level deep - array mutations, not nested objects
2 Two levels deep
n N levels deep

Fix

Step 1: Ensure Vue 3.5+

npm install vue@^3.5.0

Step 2: Use numeric depth

import { watch, ref } from 'vue'

const items = ref([{ id: 1, data: { nested: 'value' } }])

// Watch array mutations only (push, pop, etc.)
watch(items, (newItems) => {
  console.log('Array mutated')
}, { deep: 1 })

// Won't trigger on: items.value[0].data.nested = 'new'
// Will trigger on: items.value.push(newItem)

Performance Comparison

const largeNestedData = ref({ /* deeply nested structure */ })

// SLOW - traverses entire structure
watch(largeNestedData, handler, { deep: true })

// FAST - only watches top-level changes
watch(largeNestedData, handler, { deep: 1 })

// FASTEST - only reference changes
watch(largeNestedData, handler, { deep: false })

Alternative: watchEffect for Selective Tracking

// Only tracks properties actually accessed
watchEffect(() => {
  // Only re-runs when items.value.length or first item changes
  console.log(items.value.length, items.value[0]?.id)
})

TypeScript Note

If TypeScript complains about numeric deep, ensure:

  1. Vue version is 3.5+
  2. TypeScript version is current (types are included with vue package)
  3. tsconfig targets correct node_modules types

Reference: Vue 3.5 Release Notes


onWatcherCleanup

Vue 3.5 introduced onWatcherCleanup() for registering cleanup callbacks in watchers. This simplifies managing side effects like aborting requests, clearing timers, or cleaning up subscriptions.

Basic Usage

import { watch, onWatcherCleanup } from 'vue'

watch(searchQuery, async (query) => {
  const controller = new AbortController()

  // Register cleanup - runs when watcher re-runs or component unmounts
  onWatcherCleanup(() => {
    controller.abort()
  })

  const response = await fetch(`/api/search?q=${query}`, {
    signal: controller.signal
  })
  results.value = await response.json()
})

In Nested Functions

Unlike the onCleanup parameter, onWatcherCleanup can be used in nested functions:

watch(id, (newId) => {
  startPolling(newId)
})

function startPolling(id: string) {
  const interval = setInterval(() => {
    fetchData(id)
  }, 5000)

  // Works even though it's not directly in the watch callback
  onWatcherCleanup(() => {
    clearInterval(interval)
  })
}

With watchEffect

import { watchEffect, onWatcherCleanup } from 'vue'

watchEffect(() => {
  const connection = createWebSocket(url.value)

  onWatcherCleanup(() => {
    connection.close()
  })
})

Comparison with onCleanup Parameter

// Old way (still valid)
watch(source, async (value, oldValue, onCleanup) => {
  const controller = new AbortController()
  onCleanup(() => controller.abort())
  // ...
})

// New way (Vue 3.5+)
watch(source, async (value) => {
  const controller = new AbortController()
  onWatcherCleanup(() => controller.abort())
  // ...
})

Suppress Warnings

If called outside an active watcher, it logs a warning. Suppress with second parameter:

// Suppress "onWatcherCleanup called outside of watcher" warning
onWatcherCleanup(() => cleanup(), true)

Reference: Vue 3.5 Release Notes


useId for SSR

Vue 3.5 introduces useId() to generate unique, SSR-stable IDs for accessibility attributes and form elements.

Basic Usage

<script setup>
import { useId } from 'vue'

const inputId = useId()
</script>

<template>
  <label :for="inputId">Email</label>
  <input :id="inputId" type="email" />
</template>

Why useId?

  • SSR-safe: IDs are stable across server and client renders, preventing hydration mismatches
  • Unique per component instance: Multiple instances get different IDs
  • Accessible: Perfect for aria-labelledby, aria-describedby, form labels

Multiple IDs

<script setup>
import { useId } from 'vue'

const nameId = useId()
const emailId = useId()
const errorId = useId()
</script>

<template>
  <div>
    <label :for="nameId">Name</label>
    <input :id="nameId" :aria-describedby="errorId" />
    <span :id="errorId" class="error">Required</span>
  </div>
</template>

Compared to Random IDs

// ❌ Causes hydration mismatch in SSR
const id = `input-${Math.random().toString(36).slice(2)}`

// ✅ Stable across server/client
const id = useId()

Reference: Vue 3.5 Release Notes


Deferred Teleport

Vue 3.5 introduces the defer prop for <Teleport> which mounts content after the current render cycle, solving timing issues with dynamic targets.

Problem

<!-- ❌ Target doesn't exist yet during first render -->
<Teleport to="#modal-container">
  <Modal />
</Teleport>

<div id="modal-container"></div>

Solution

<!-- ✅ Defers teleport until after current render cycle -->
<Teleport to="#modal-container" defer>
  <Modal />
</Teleport>

<div id="modal-container"></div>

Use Cases

  • Teleporting to elements rendered later in the same component
  • Dynamic portals that may not exist immediately
  • Nested component structures where target is created by a child

Important Notes

  • defer is opt-in for backwards compatibility
  • The teleported content renders in the next tick
  • Target element must exist by the time deferred teleport executes

Reference: Vue 3.5 Release Notes

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning2mo5 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    This skill provides comprehensive best practices for Vue 3 development, including TypeScript integration, component design patterns, testing strategies using Pinia and Vitest, and tooling configuration for Vite and Volar. All referenced resources and packages are standard ecosystem tools from reputable sources.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

  • Runlayer9mo

    6/6 files flagged

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 6e805e4. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 10 months ago
metadata
{
  "author": "ejirocodes",
  "version": "1.0.0"
}

README badge

README badge for ejirocodes/agent-skills/vue-best-practices