A Beginner’s Guide

Vue.js Basics: A Beginner’s Guide

Vue.js is a progressive JavaScript framework used to build interactive web interfaces and single-page applications (SPAs). It is approachable, versatile, and perfect for both beginners and experienced developers looking to enhance their frontend skills.




1. Introduction to Vue.js

Vue.js focuses on the view layer of your application. It allows you to build dynamic web pages easily and integrates well with other libraries or existing projects.

Why learn Vue.js?

  • Simple and clear syntax suitable for beginners.
  • Reactive data binding for automatic UI updates.
  • Component-based architecture for reusable code.
  • Great ecosystem for modern web applications.



2. Setting Up Vue.js

You can start using Vue.js in multiple ways, depending on your project size and tools.

Option 1: CDN (Quick Start)

<!DOCTYPE html>
<html>
<head>
  <title>Vue App</title>
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
  <div id="app">{{ message }}</div>

  <script>
    const app = Vue.createApp({
      data() {
        return {
          message: 'Hello, Vue!'
        }
      }
    })
    app.mount('#app')
  </script>
</body>
</html>


Option 2: Vue CLI (Professional Setup)

Install Vue CLI globally and scaffold a project:

# Install Vue CLI
npm install -g @vue/cli

# Create a new project
vue create my-vue-app

# Run the development server
cd my-vue-app
npm run serve


3. Vue.js Template Syntax

Vue templates allow you to bind data directly to the HTML with a simple syntax.

Interpolation:

<div id="app">
  <p>{{ message }}</p>
</div>

Directives:

Directives are special attributes prefixed with v- that provide dynamic behavior.


<div id="app">
  <p v-if="showMessage">This message is visible</p>
  <p v-else>Message is hidden</p>
</div>

Example with Loops:

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


4. Vue.js Components

Components let you split your application into reusable pieces. Each component has its own template, logic, and styling.

<!-- Parent Component -->
<div id="app">
  <hello-world></hello-world>
</div>

<script>
  const HelloWorld = {
    template: '<p>Hello from component!</p>'
  }

  const app = Vue.createApp({})
  app.component('hello-world', HelloWorld)
  app.mount('#app')
</script>

Tip: Start with small components and gradually build complex UIs.




5. Handling Events

Vue allows you to handle user events easily using v-on or its shorthand @.

<div id="app">
  <button @click="sayHello">Click Me</button>
</div>

<script>
  const app = Vue.createApp({
    methods: {
      sayHello() {
        alert('Hello from Vue!');
      }
    }
  })
  app.mount('#app')
</script>

Tip: Use methods for event handling and keep your template clean.




6. Vue.js Reactive Data

One of Vue’s core strengths is its reactivity system. When data changes, the UI updates automatically.

<div id="app">
  <input v-model="name" placeholder="Enter your name">
  <p>Hello, {{ name }}!</p>
</div>

<script>
  const app = Vue.createApp({
    data() {
      return {
        name: ''
      }
    }
  })
  app.mount('#app')
</script>

Tip: Use v-model for two-way data binding in forms and inputs.




7. Summary and Next Steps

In this beginner-friendly guide, we covered:

  • Introduction to Vue.js and its benefits.
  • Setting up Vue via CDN and Vue CLI.
  • Template syntax: interpolation, directives, loops.
  • Building and using components.
  • Handling events and reactive data with v-model.

Next Steps:

  • Practice by building simple interactive apps like a to-do list.
  • Learn about Vue Router for navigation.
  • Explore Vuex or Pinia for state management in larger apps.
  • Experiment with dynamic forms and API calls using Axios.

Vue.js makes frontend development approachable and fun. Start building, experiment often, and gradually advance to more complex applications!