vue
✓Verified·Scanned 2/18/2026
Avoid common Vue mistakes — reactivity traps, ref vs reactive, computed timing, and Composition API pitfalls.
from clawhub.ai·v7ad33cf·3.3 KB·1 installs
Scanned from 1.0.0 at 7ad33cf · Transparency log ↗
$ vett add clawhub.ai/ivangdavila/vue
Reactivity System
reffor primitives — access with.valuein script, auto-unwrapped in templatereactivefor objects — no.value, but can't reassign whole object- Destructuring
reactiveloses reactivity — usetoRefs(state)to preserve - Array index assignment reactive in Vue 3 —
arr[0] = xworks, unlike Vue 2
ref vs reactive
refcan hold any value — including objects,.valuealways needed in scriptreactiveonly for objects — returns Proxy, same referencerefunwraps in template —{{ count }}not{{ count.value }}- Nested refs unwrap inside reactive —
reactive({ count: ref(0) }).countis number
Computed and Watch
computedis cached — only recalculates when dependencies changecomputedshould be pure — no side effects, usewatchfor effectswatchlazy by default —immediate: truefor initial runwatchEffectruns immediately — auto-tracks dependencies, no need to specify
Watch Pitfalls
- Watching reactive object needs deep —
watch(state, cb, { deep: true })orwatch(() => state.prop, cb) - Watch callback receives old/new —
watch(source, (newVal, oldVal) => {}) watchEffectcan't access old value — usewatchif needed- Stop watcher with returned function —
const stop = watch(...); stop()
Props and Emits
definePropsfor type-safe props —defineProps<{ msg: string }>()- Props are readonly — don't mutate, emit event to parent
defineEmitsfor type-safe events —defineEmits<{ (e: 'update', val: string): void }>()v-modelis:modelValue+@update:modelValue— custom v-model withdefineModel()
Template Refs
ref="name"+const name = ref(null)— must match name- Available after mount — access in
onMounted, not setup body refon component = component instance —refon element = DOM element- Template ref with v-for —
refbecomes array
Lifecycle Hooks
onMountedfor DOM access — component mounted to DOMonUnmountedfor cleanup — subscriptions, timersonBeforeMountruns before DOM insert — rarely needed- Hooks must be called in setup — not in callbacks or conditionals
Provide/Inject
provide('key', value)in parent —inject('key')in any descendant- Reactive if value is ref/reactive — otherwise static
- Default value:
inject('key', defaultVal)— third param for factory - Symbol keys for type safety — avoid string collisions
Vue Router
useRoutefor current route — reactive, use in setupuseRouterfor navigation —router.push('/path')- Navigation guards:
beforeEach,beforeResolve,afterEach— returnfalseto cancel <RouterView>with named views — multiple views per route
Common Mistakes
- Async setup needs
<Suspense>—async setup()component must be wrapped v-ifvsv-show— v-if removes from DOM, v-show toggles display- Key on v-for required —
v-for="item in items" :key="item.id" - Event modifiers order matters —
.prevent.stopvs.stop.prevent - Teleport for modals —
<Teleport to="body">renders outside component tree