Concept Explanation
Vue is the framework people reach for when React feels like too much. Instead of mixing HTML into JavaScript (JSX), you write normal HTML templates and sprinkle in a little "magic" with directives — special v- attributes.
Vue is also progressive: you can drop it into a single <div> on an old website, or build a giant app with it. You adopt only as much as you need.
Syntax or Core Structure
The Template System
<template>
<div id="counter">
<p>Count is: {{ count }}</p>
<button @click="increment">+1</button>
<p v-if="count > 10">Wow, that's high!</p>
</div>
</template>
<script setup>
import { ref } from 'vue';
const count = ref(0); // reactive state
const increment = () => count.value++;
</script>
{{ count }} prints a value, @click listens for events, and v-if shows or hides an element — all inside plain HTML.
Key Features
- Directives:
v-iffor logic,v-forfor loops,v-modelfor two-way form binding. - Reactivity: change a
refand Vue updates only the exact piece of the page that used it. - Single File Components: the HTML, CSS, and JS for one component live together in a
.vuefile.
Real World Example
GitLab, Nintendo, and BMW use Vue for dashboards and marketing sites. Its gentle learning curve makes it a favourite for teams that want React-style power without the React-style ceremony — you can be productive on day one with just HTML knowledge.
Common Mistakes
- Forgetting
.value: In<script setup>, arefkeeps its data in.value. Writecount.value++, notcount++. - Mutating props: A child component must not change data handed down by its parent. Emit an event and let the parent update it.
v-ifvsv-show:v-ifremoves the element entirely;v-showjust hides it with CSS. Usev-showfor things toggled often.
Best Practices
- Prefer
<script setup>: it's the modern, least-boilerplate way to write Vue 3 components. - Key your
v-for: always add:keyso Vue can track list items efficiently. - Keep components small: one job per
.vuefile, just like functions.
Practice Exercises
- The Toggle: Write a Vue component using
refandv-ifthat shows or hides a paragraph when a button is clicked. - The List: Use
v-forto render an array of five tasks, each with a:key. - The Comparison: List two things Vue does differently from React (e.g. templates vs JSX,
refvsuseState).