Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Vue.js Documentation

The approachable frontend framework — components, reactivity, directives, composables, Vue Router and Pinia.

Vue is a progressive JavaScript framework for building user interfaces. It is known for being easy to learn: HTML-like templates, reactive data and single-file components. This page uses Vue 3 with the Composition API. You should know JavaScript first.

1. Introduction

Feature Meaning
Reactivity Change the data and the page updates itself
Single-file components Template, logic and style in one .vue file
Directives Special attributes such as v-if and v-for
Ecosystem Vue Router for pages, Pinia for shared state, Nuxt for full-stack apps

2. Setup

npm create vue@latest my-app
cd my-app
npm install
npm run dev

3. Your First Component

<script setup>
import { ref } from 'vue';

const count = ref(0);
const add = () => count.value++;
</script>

<template>
  <h1>Count: {{ count }}</h1>
  <button @click="add">Add</button>
</template>

<style scoped>
h1 { color: teal; }
</style>

<script setup> is the short modern syntax, and scoped keeps the styles inside this component.

4. Reactivity

import { ref, reactive, computed, watch } from 'vue';

const name = ref('Asha');                 // any value; use .value in script
const form = reactive({ email: '', age: 20 });  // objects

const upper = computed(() => name.value.toUpperCase());   // cached, updates by itself

watch(name, (now, before) => {
  console.log(`changed from ${before} to ${now}`);
});

In the template, refs unwrap on their own: write {{ name }}, not {{ name.value }}.

Common mistake: forgetting .value in the script, or destructuring a reactive object, which loses reactivity.

5. Template Directives

<p v-if="marks >= 40">Pass</p>
<p v-else>Fail</p>

<p v-show="open">Hidden with CSS, not removed</p>

<ul>
  <li v-for="s in students" :key="s.id">{{ s.name }}</li>
</ul>

<img :src="photo" :alt="name" />
<button :disabled="busy" @click="save">Save</button>
<div :class="{ active: isActive }"></div>

<input v-model="name" />
<form @submit.prevent="send"></form>
Directive Short form Does
v-bind:src :src Binds an attribute
v-on:click @click Listens to an event
v-model — Two-way binding for inputs
v-if / v-show — Remove from the page / hide with CSS
v-for — Repeat; always add :key

6. Components, Props and Events

<!-- StudentCard.vue -->
<script setup>
const props = defineProps({
  name: { type: String, required: true },
  marks: { type: Number, default: 0 },
});
const emit = defineEmits(['remove']);
</script>

<template>
  <div>
    {{ name }} — {{ marks }}
    <button @click="emit('remove', name)">Remove</button>
  </div>
</template>
<StudentCard name="Asha" :marks="91" @remove="onRemove" />

Data goes down through props, and messages come up through events. <slot /> lets a parent pass in HTML.

7. Lifecycle Hooks and Fetching Data

<script setup>
import { ref, onMounted } from 'vue';

const posts = ref([]);
const loading = ref(true);
const error = ref('');

onMounted(async () => {
  try {
    const res = await fetch('https://jsonplaceholder.typicode.com/posts');
    posts.value = await res.json();
  } catch (e) {
    error.value = e.message;
  } finally {
    loading.value = false;
  }
});
</script>

<template>
  <p v-if="loading">Loading…</p>
  <p v-else-if="error">{{ error }}</p>
  <ul v-else>
    <li v-for="p in posts" :key="p.id">{{ p.title }}</li>
  </ul>
</template>

Common hooks: onMounted, onUpdated, onUnmounted (clean up timers here).

8. Composables

A composable is a reusable function with reactive state, like a custom hook in React.

// useCounter.js
import { ref } from 'vue';

export function useCounter(start = 0) {
  const count = ref(start);
  const add = () => count.value++;
  return { count, add };
}

9. Vue Router

import { createRouter, createWebHistory } from 'vue-router';

export const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', component: Home },
    { path: '/courses/:id', component: Course, props: true },
  ],
});
<RouterLink to="/courses/7">Open</RouterLink>
<RouterView />

In script: useRoute().params.id and useRouter().push('/').

10. Pinia — Shared State

import { defineStore } from 'pinia';
import { ref, computed } from 'vue';

export const useCart = defineStore('cart', () => {
  const items = ref([]);
  const total = computed(() => items.value.length);
  const add = (item) => items.value.push(item);
  return { items, total, add };
});
const cart = useCart();
cart.add('Book');

11. Vue vs React vs Angular

Vue React Angular
Type Framework (progressive) UI library Full framework
Templates HTML-based JSX HTML-based
Language JS or TS JS or TS TypeScript
Learning curve Gentle Medium Steep
Routing, state Official packages Community choices Built in

12. Practice Problems

  1. A counter with a step input (ref, v-model).
  2. A to-do list with add, delete, and a filter using computed.
  3. A useFetch composable with loading and error states.
  4. A two-page app with Vue Router and a detail page.
  5. A shopping cart with Pinia.

13. Interview Questions

ref vs reactive? ref wraps any value and uses .value; reactive makes an object reactive.

computed vs watch? computed derives a cached value; watch runs side effects when something changes.

v-if vs v-show? v-if adds and removes the element; v-show toggles CSS display.

Why is :key needed in v-for? So Vue can track items and update them correctly.

How do parent and child talk? Props down, events up. Use Pinia for distant components.

What is a composable? A function that reuses stateful logic with the Composition API.

Options API vs Composition API? Options groups code by type (data, methods); Composition groups it by feature and reuses it more easily.

How does Vue 3 reactivity work? With JavaScript Proxies that track reads and trigger updates on writes.

14. Cheat Sheet & References

Reactivity

const n = ref(0);          // n.value in script
const o = reactive({});
const c = computed(() => n.value * 2);
watch(n, (v) => {});
onMounted(() => {});

Template

{{ value }}
<p v-if="a"></p> <p v-else></p>
<li v-for="x in list" :key="x.id"></li>
<input v-model="name" />
<a :href="url" @click="go"></a>
<form @submit.prevent="send"></form>

Components

const props = defineProps({ title: String });
const emit = defineEmits(['save']);
emit('save', data);

References