No history yet

Introduction to Vue.js

What is Vue.js?

Vue.js is a JavaScript framework for building user interfaces. Think of it as a toolbox for creating the parts of a website that users see and interact with, from simple buttons to complex, dynamic sections.

Vue.js is an open-source JavaScript framework primarily used for building user interfaces and single-page applications (SPAs).

One of Vue's core ideas is that it's a progressive framework. This means you can adopt it incrementally. You can use Vue to enhance a small part of an existing website, or you can use it to build a massive, complex single-page application from the ground up. It's like a set of building blocks. You can start with just a few basic pieces, and as your project grows, you can bring in more advanced blocks to handle more complexity.

The key takeaway is flexibility. Vue scales with your needs, making it approachable for beginners and powerful for experts.

Vue in the Framework World

You've likely heard of other JavaScript frameworks like React and Angular. So, where does Vue fit in?

If Angular is a complete car kit with a detailed instruction manual, and React is a high-performance engine you can build a car around, Vue is somewhere in the middle. It provides a solid frame and engine but lets you choose many of the other parts. It offers more structure and built-in features than React out of the box but is less rigid and opinionated than Angular. This balance makes it famously easy to learn.

Lesson image

Because of its gentle learning curve and excellent documentation, many developers find Vue to be a great first framework. You only need a solid grasp of HTML, CSS, and JavaScript to get started.

Setting Up Your Workspace

The modern way to build a Vue application is with a tool called Vite. It sets up a development server and bundles your code for production, making the whole process fast and efficient. To use it, you'll need Node.js installed on your computer.

Open your terminal and run this command:

npm create vue@latest

This command kicks off an interactive scaffolding process. It will ask you a few questions, like your project's name and if you want to add tools like TypeScript or Pinia. For now, you can stick with the defaults by just pressing Enter for each question.

Once it's done, navigate into your new project directory, install the necessary dependencies, and start the development server:

# Move into your new project folder
cd <your-project-name>

# Install the project dependencies
npm install

# Start the development server
npm run dev

Your terminal will show you a local URL, usually http://localhost:5173. Open that in your browser, and you'll see your brand-new Vue application running!

Your First Vue Component

Let's look at the basic structure of a Vue application. In your project folder, open the src directory and then the App.vue file. This is your main application component. Vue applications are built by composing these small, reusable pieces called components.

A typical .vue file, known as a Single-File Component (SFC), has three parts:

PartPurpose
<template>Contains the HTML structure of the component.
<script>Holds the JavaScript logic, such as data and methods.
<style>Defines the CSS styles for the component.

Let's modify App.vue to make a simple counter. Replace the contents of the file with this code:

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

// reactive state
const count = ref(0)

// function to increment the count
function increment() {
  count.value++
}
</script>

<template>
  <h1>My First Vue Counter</h1>
  <button @click="increment">
    Count is: {{ count }}
  </button>
</template>

<style scoped>
button {
  font-size: 1.2rem;
  margin-top: 10px;
}
</style>

Let's break this down:

  1. <script setup>: We import ref from Vue to create a reactive variable called count. "Reactive" means that if count changes, Vue will automatically update any part of the template that uses it.
  2. <template>: We have a button with an event listener, @click="increment". When clicked, it calls the increment function. The text inside the button displays the current value of count using double curly braces: {{ count }}. This is how Vue binds data to the template.
  3. <style scoped>: The scoped attribute means these CSS styles will only apply to this component, preventing them from accidentally affecting other parts of your application.

Save the file, and your browser should automatically update. You now have an interactive counter. You've just seen the core of Vue in action: reactive data powering a dynamic user interface.

Quiz Questions 1/5

What does it mean that Vue.js is a 'progressive' framework?

Quiz Questions 2/5

What command is used to start the scaffolding process for a new Vue project using Vite?

That's your first step into the world of Vue.js. You've learned what it is, how it fits into the web development landscape, and how to build a simple interactive component.