Skip to content

project rulesFrontendvuetypescript

Vue 3 Composition Rules

Project rules for Vue 3: script setup with TypeScript, composables, Pinia stores, typed props and emits, and Vitest component tests.

npx workspacetemplates add vue3-composition-rules --tool claude-code

Writes 1 file to your project root

CLAUDE.mdadd to your file20 lines · 1.4 KBFile
# Project rules: Vue 3 with Composition API

## Components
- Use `<script setup lang="ts">` in every single-file component. Do not write new Options API components or mixins.
- Type props with `defineProps<{ ... }>()` and defaults with `withDefaults`. Type events with `defineEmits<{ (e: 'save', id: string): void }>()`. Never mutate a prop; emit an event instead.
- Use `v-model` through `defineModel` for two-way bindings on form components.
- Give `v-for` a stable `:key`, and never put `v-if` and `v-for` on the same element.
- Component files are PascalCase and multi-word, with `Base` or `App` prefixes for shared primitives.

## Reactivity
- Prefer `ref` for values and `computed` for derived state. Use `watch` only for side effects, with `watchEffect` for simple cases.
- Do not destructure a reactive object without `toRefs` or `storeToRefs`; it loses reactivity.

## Composables and state
- Put reusable stateful logic in `composables/useThing.ts`, return refs and functions, and clean up listeners in `onScopeDispose` or `onUnmounted`.
- Use Pinia setup stores for cross-component state, one store per domain. Keep server data and local form state out of global stores when a composable does the job.

## Testing and checks
- Test with Vitest and Vue Test Utils: mount, act through the DOM, assert on rendered output and emitted events.
- Run `vue-tsc --noEmit`, the ESLint run and `vitest run` before finishing.

What it does

This file steers an agent toward modern Vue 3 code: single-file components with script setup, composables for shared logic and Pinia for state that really is global.

It rules out older patterns such as the Options API and mixins, and describes how to type props, emits and stores so templates are checked by the compiler.

Example

you › Add a reusable tag input that supports v-model

agent › The agent builds TagInput.vue with script setup, defineModel and typed emits, a useTags composable for the add and remove logic, and tests for keyboard entry and duplicate tags.

Questions

Can I keep some Options API components?
Existing ones can stay. The rule applies to new files, so the codebase moves one way over time.
Why avoid destructuring reactive objects?
The copied values stop updating when the source changes. toRefs and storeToRefs keep the link alive.
  • .NET Blazor Rules

    Project rulescsharp

    Free
  • Angular Rules

    Project rulesangular

    Free
  • Astro Rules

    Project rulesastro

    Free
  • Electron Rules

    Project ruleselectron

    Free