Vue.js Quick Reference
This reference condenses the syntax you reach for most often when writing Vue 3 with the Composition API and <script setup> — reactivity primitives, template directives, lifecycle hooks, and component communication — into one scannable page. It intentionally favors terse, correct snippets over lengthy explanation; use the other study-notes topics in this course for the deeper reasoning behind each API.
Cricket analogy: Like a laminated cheat sheet of field placements a captain keeps in a pocket during a match, this page condenses ref, directives, and lifecycle hooks into quick-glance snippets rather than explaining the strategy behind each field setting.
Reactivity primitives
import { ref, reactive, computed, watch, watchEffect, toRefs } from 'vue'
const count = ref(0) // count.value to read/write
const user = reactive({ name: 'Ada', age: 30 }) // access as user.name
const doubled = computed(() => count.value * 2) // cached, read-only by default
watch(count, (newVal, oldVal) => {
console.log(`count: ${oldVal} -> ${newVal}`)
})
watchEffect(() => {
console.log(`count is now ${count.value}`) // runs immediately + on change
})
const { name, age } = toRefs(user) // preserves reactivity when destructuringTemplate directives
<template>
<p v-if="isLoggedIn">Welcome back</p>
<p v-else-if="isLoading">Loading…</p>
<p v-else>Please log in</p>
<li v-for="item in items" :key="item.id">{{ item.label }}</li>
<button @click="handleSave">Save</button>
<input v-model="searchQuery" placeholder="Search…" />
<img :src="avatarUrl" :alt="user.name" />
<div v-show="isVisible">Toggled with display: none</div>
</template>Component communication and lifecycle hooks
<!-- Child.vue -->
<script setup>
import { onMounted, onUnmounted } from 'vue'
const props = defineProps({
label: { type: String, required: true },
count: { type: Number, default: 0 },
})
const emit = defineEmits(['update:count'])
function increment() {
emit('update:count', props.count + 1)
}
onMounted(() => console.log('DOM is ready'))
onUnmounted(() => console.log('cleanup: remove listeners, cancel timers'))
</script>
<template>
<button @click="increment">{{ label }}: {{ count }}</button>
</template>
<!-- Parent.vue -->
<!-- <Child :label="'Clicks'" v-model:count="clickCount" /> -->Composition API lifecycle hooks are imported functions called inside setup() / <script setup>, not object options: onMounted, onUpdated, onUnmounted, onBeforeMount, onBeforeUpdate, onBeforeUnmount, plus onActivated/onDeactivated for components inside <KeepAlive>. Note there is no onCreated — code that would run in Options API's created hook simply goes directly in the top level of setup(), since it already runs at that point in the lifecycle.
Quick gotchas to keep in mind while scanning this reference: reactive() does not work on primitives — use ref() instead; destructuring a reactive() object loses reactivity unless wrapped in toRefs(); :key in v-for should be a stable id, never the array index for mutable lists; and props must never be mutated directly inside a child component. For state shared beyond a single component tree, provide/inject handles arbitrary-depth passing without prop drilling, while Pinia (defineStore) is the standard choice for real cross-cutting application state.
- Reactivity:
ref()for any value type (access via.value),reactive()for objects only (direct property access). computed()is cached and dependency-tracked;watch/watchEffectrun side effects in response to reactive changes.- Core directives:
v-if/v-else,v-forwith:key,v-model,v-bind(:attr),v-on(@event), andv-show. definePropsanddefineEmitsdeclare a component's public interface for props in and events out.- Lifecycle hooks (
onMounted,onUpdated,onUnmounted, etc.) are imported functions called inside<script setup>. - Escalate state sharing only as needed: local state, then props/emit, then provide/inject, then a Pinia store.
Practice what you learned
1. Which reactivity primitive should be used to wrap a single primitive number that will be reassigned over time?
2. In `<script setup>`, which pair of functions declares a component's props and the custom events it can emit?
3. Which Composition API lifecycle hook does NOT exist because its Options API equivalent runs at a point already covered by plain `setup()` code?
4. What is the difference between `v-show` and `v-if` as referenced in this quick-reference guide?
5. According to the recommended escalation order for sharing state, what should be tried before reaching for a Pinia store?
Was this page helpful?
You May Also Like
Vue.js Interview Questions
A curated set of commonly asked Vue 3 interview questions with model answers, covering reactivity, the Composition API, component communication, and performance.
Common Vue.js Pitfalls
Surveys the most frequent mistakes Vue 3 developers make around reactivity, prop mutation, key usage in lists, and lifecycle timing, with guidance on how to avoid each.
The Composition API vs Options API
Compares Vue's two component authoring styles, explaining why the Composition API was introduced and when each approach makes sense in real projects.
The setup() Function and <script setup>
Learn the entry point of the Composition API — the setup() function — and how <script setup> compiles away its boilerplate.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics