Frameworks & Libraries

Vue.js Basics

Vue is a progressive JavaScript framework loved for its gentle learning curve, excellent documentation, and elegant reactivity system. Learn Single File Components, the Composition API, directives, and Pinia state management.

What is Vue.js?

Vue is a progressive JavaScript framework for building user interfaces. Single-file components (.vue) combine a template, script and styles. Reactive state from ref() and reactive() updates the DOM automatically. Vue is known for a gentle learning curve and clear documentation, and Nuxt is its full-stack framework.

<script setup>
import { ref, computed } from "vue";
const count = ref(0);
const double = computed(() => count.value * 2);
</script>

<template>
  <button @click="count++">{{ count }} (double: {{ double }})</button>
</template>

React vs Vue — Quick Reference

ConceptReactVue 3
Local stateuseState()ref() / reactive()
Derived valueuseMemo()computed()
Side effectsuseEffect()watch() / watchEffect()
Two-way bindingvalue + onChangev-model
ConditionalTernary / &&v-if / v-else / v-show
Lists.map() + key propv-for + :key
EventsonClick={fn}@click="fn"
Lifecycle (mount)useEffect(fn, [])onMounted(fn)
Global stateRedux / ZustandPinia
RoutingReact RouterVue Router
Custom hooksCustom hooks (use...)Composables (use...)
StylingCSS modules / styledscoped CSS in .vue

Common Pitfalls

Forgetting .value when accessing ref() in script — ref.count++ instead of ref.value++
Only in templates does Vue auto-unwrap refs. In <script setup>, always use count.value
Using v-if and v-for on the same element — v-for takes priority in Vue 2, causing unexpected behavior
Wrap the v-if in a parent <template> tag: <template v-if='show'><li v-for='...'>
Mutating props directly in a child component — Vue warns and it breaks data flow
Emit an event to the parent: emit('update:value', newVal). The parent updates its own state.
Destructuring reactive() objects breaks reactivity: const { name } = reactive(state)
Use toRefs() to destructure reactively: const { name } = toRefs(state)

Vue Code Examples

Code Editor

<div id="app">
  <input v-model="name" placeholder="Your name">
  <p>Hello, {{ name || "stranger" }}!</p>
  <button @click="count++">Clicked {{ count }} times (double: {{ double }})</button>
  <ul><li v-for="t in todos" :key="t">{{ t }}</li></ul>
</div>

Preview

Vue Basics: summary

Key points

  • ref values are read and written with .value in script, but used directly in templates.
  • Directives: v-if, v-for, v-model (two-way binding), @event, :prop.
  • computed caches derived values; watch runs side effects when data changes.

Frequently asked questions

›Vue or React?

Both are excellent. React has the bigger job market and ecosystem; Vue has a gentler learning curve and more built-in conventions. Either is fine as a first framework.