Vue.js: The Approachable Choice

Vue.js: The Approachable Choice in JavaScript: Vue is the framework people reach for when React feels like too much.

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-if for logic, v-for for loops, v-model for two-way form binding.
  • Reactivity: change a ref and 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 .vue file.

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

  1. Forgetting .value: In <script setup>, a ref keeps its data in .value. Write count.value++, not count++.
  2. Mutating props: A child component must not change data handed down by its parent. Emit an event and let the parent update it.
  3. v-if vs v-show: v-if removes the element entirely; v-show just hides it with CSS. Use v-show for 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 :key so Vue can track list items efficiently.
  • Keep components small: one job per .vue file, just like functions.

Practice Exercises

  1. The Toggle: Write a Vue component using ref and v-if that shows or hides a paragraph when a button is clicked.
  2. The List: Use v-for to render an array of five tasks, each with a :key.
  3. The Comparison: List two things Vue does differently from React (e.g. templates vs JSX, ref vs useState).