All skills
vuejs-ai avatar

/vue-best-practices

@b9d14d0 official
by Vue AIvuejs-ai/skills2.9k stars
167

MUST be used for Vue.js tasks. Strongly recommends Composition API with `<script setup>` and TypeScript as the standard approach. Covers Vue 3, SSR, Volar, vue-tsc. Load for any Vue, .vue files, Vue Router, Pinia, or Vite with Vue work. ALWAYS use Composition API unless the project explicitly requires Options API.

Use this Skill: https://skilld.dev/gh/vuejs-ai/skills/vue-best-practices

This session only. Nothing lands on disk.

referencesperf-v-once-v-memo-directives.md

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

Use v-once and v-memo to Skip Unnecessary Updates

Impact: MEDIUM - Vue re-evaluates templates on every reactive change. For content that never changes or changes infrequently, v-once and v-memo tell Vue to skip updates, reducing render work.

Use v-once for truly static content and v-memo for conditionally-static content in lists.

Task List

  • Apply v-once to elements that use runtime data but never need updating
  • Apply v-memo to list items that should only update on specific condition changes
  • Verify memoized content doesn't need to respond to other state changes
  • Profile with Vue DevTools to confirm update skipping

v-once: Render Once, Never Update

BAD:

<template>
  <!-- BAD: Re-evaluated on every parent re-render -->
  <div class="terms-content">
    <h1>Terms of Service</h1>
    <p>Version: {{ termsVersion }}</p>
    <div v-html="termsContent"></div>
  </div>

  <!-- This content NEVER changes, but Vue checks it every render -->
  <footer>
    <p>Copyright {{ copyrightYear }} {{ companyName }}</p>
  </footer>
</template>

GOOD:

<template>
  <!-- GOOD: Rendered once, skipped on all future updates -->
  <div class="terms-content" v-once>
    <h1>Terms of Service</h1>
    <p>Version: {{ termsVersion }}</p>
    <div v-html="termsContent"></div>
  </div>

  <!-- v-once tells Vue this never needs to update -->
  <footer v-once>
    <p>Copyright {{ copyrightYear }} {{ companyName }}</p>
  </footer>
</template>

<script setup>
// These values are set once at component creation
const termsVersion = '2.1'
const termsContent = fetchedTermsHTML
const copyrightYear = 2024
const companyName = 'Acme Corp'
</script>

v-memo: Conditional Memoization for Lists

BAD:

<template>
  <!-- BAD: All items re-render when selectedId changes -->
  <div v-for="item in list" :key="item.id">
    <div :class="{ selected: item.id === selectedId }">
      <ExpensiveComponent :data="item" />
    </div>
  </div>
</template>

GOOD:

<template>
  <!-- GOOD: Items only re-render when their selection state changes -->
  <div
    v-for="item in list"
    :key="item.id"
    v-memo="[item.id === selectedId]"
  >
    <div :class="{ selected: item.id === selectedId }">
      <ExpensiveComponent :data="item" />
    </div>
  </div>
</template>

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

const list = ref([/* many items */])
const selectedId = ref(null)

// When selectedId changes:
// - Only the previously-selected item re-renders (selected: true -> false)
// - Only the newly-selected item re-renders (selected: false -> true)
// - All other items are SKIPPED (v-memo values unchanged)
</script>

v-memo with Multiple Dependencies

<template>
  <!-- Re-render only when item's selection OR editing state changes -->
  <div
    v-for="item in items"
    :key="item.id"
    v-memo="[item.id === selectedId, item.id === editingId]"
  >
    <ItemCard
      :item="item"
      :selected="item.id === selectedId"
      :editing="item.id === editingId"
    />
  </div>
</template>

<script setup>
const selectedId = ref(null)
const editingId = ref(null)
const items = ref([/* ... */])
</script>

v-memo with Empty Array = v-once

<template>
  <!-- v-memo="[]" is equivalent to v-once -->
  <div v-for="item in staticList" :key="item.id" v-memo="[]">
    {{ item.name }}
  </div>
</template>

When NOT to Use These Directives

<template>
  <!-- DON'T: Content that DOES need to update -->
  <div v-once>
    <span>Count: {{ count }}</span>  <!-- count won't update! -->
  </div>

  <!-- DON'T: When child components have their own reactive state -->
  <div v-memo="[selected]">
    <InputField v-model="item.name" />  <!-- v-model won't work properly -->
  </div>

  <!-- DON'T: When the memoization benefit is minimal -->
  <span v-once>{{ simpleText }}</span>  <!-- Overhead not worth it -->
</template>

Performance Comparison

Scenario Without Directive With v-once/v-memo
Static header, parent re-renders 100x Re-evaluated 100x Evaluated 1x
1000 items, selection changes 1000 items re-render 2 items re-render
Complex child component Full re-render Skipped if memoized

Debugging Memoized Components

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

// This won't fire if v-memo prevents update
onUpdated(() => {
  console.log('Component updated')
})
</script>

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides a comprehensive set of best practices for Vue.js development, focusing on the Composition API, reactivity, and performance optimization. It includes standard coding guidelines and explicitly recommends security best practices such as sanitizing untrusted content to prevent XSS.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    23 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at b9d14d0. 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 8 months ago
metadata
{
  "author": "github.com/vuejs-ai",
  "version": "18.0.0"
}
  • Vue
  • TypeScript
  • composition-api
  • script-setup
  • vue-router
  • pinia
  • vite
  • ssr
  • volar
  • component-design

README badge

README badge for vuejs-ai/skills/vue-best-practices

Instructs Claude to follow Vue 3 best practices using Composition API with `<script setup>` and TypeScript, covering reactivity, component splitting, data flow, and composables. Load this skill for any Vue work—it enforces a workflow that prioritizes minimal state, explicit props/events, focused components, and defers optional features until requirements demand them.

Generated from the current SKILL.md.

Should I use Options API or Composition API?
Use Composition API with <script setup> and TypeScript as the default. Only switch to Options API if the project explicitly requires it.
Does this skill cover Vue 2?
No. This skill targets Vue 3, Composition API, <script setup>, and TypeScript.
When should I split a component into smaller components?
Split when a component has more than one clear responsibility, contains 3+ distinct UI sections, or has repeated template blocks. Keep entry/root and route view components thin unless the feature is a small throwaway demo.
What references should I read before starting a Vue task?
You must read and keep active: reactivity.md, sfc.md, component-data-flow.md, and composables.md. Load additional optional references only when requirements call for slots, animations, state management, or other specific features.
When should I add state management like Pinia?
Only when app-wide shared state crosses feature boundaries. For most features, use composables and props/events for data flow instead.

Generated from the current SKILL.md. These answers refresh after source changes.