100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
JavaScript

Vue.js Quick Reference

A condensed cheat-sheet of core Vue 3 Composition API syntax, directives, and lifecycle hooks for quick lookup while coding, rather than deep conceptual explanation.

Interview PrepBeginner7 min readJul 9, 2026
Analogies

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

javascript
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 destructuring

Template directives

vue
<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

vue
<!-- 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/watchEffect run side effects in response to reactive changes.
  • Core directives: v-if/v-else, v-for with :key, v-model, v-bind (:attr), v-on (@event), and v-show.
  • defineProps and defineEmits declare 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

Was this page helpful?

Topics covered

#JavaScript#VueJsStudyNotes#WebDevelopment#VueJsQuickReference#Vue#Quick#Reference#Reactivity#StudyNotes#SkillVeris