DEV Community

Cover image for Getting Started with Vue.js
khamal22
khamal22

Posted on

1

Getting Started with Vue.js

A Beginner's Guide

Vue.js is a progressive JavaScript framework that has gained immense popularity for building user interfaces and single-page applications (SPAs). It’s lightweight, easy to learn, and highly flexible, making it a great choice for both beginners and experienced developers. In this blog, we’ll explore the basics of Vue.js and walk through some simple code examples to get you started.

What is Vue.js?

Vue.js is a JavaScript framework designed to simplify web development. It focuses on the view layer of an application, allowing you to create dynamic, reactive, and component-based UIs. Some of its key features include:

  • Reactive Data Binding: Automatically updates the DOM when data changes.
  • Component-Based Architecture: Encourages building reusable and modular components.
  • Directives: Special attributes that add functionality to HTML elements.
  • Vue CLI: A powerful tool for scaffolding and managing Vue projects.

Setting Up Vue.js

To get started with Vue.js, you can include it via a CDN or use the Vue CLI to create a project. For simplicity, let’s use the CDN approach:

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
Enter fullscreen mode Exit fullscreen mode

Basic Vue.js Example

Let’s create a simple Vue app that displays a message and updates it when a button is clicked.

<div id="app">
  <h1>{{ message }}</h1>
  <button @click="updateMessage">Click Me!</button>
</div>

<script>
  const { createApp } = Vue;

  createApp({
    data() {
      return {
        message: "Hello, Vue.js!"
      };
    },
    methods: {
      updateMessage() {
        this.message = "You clicked the button!";
      }
    }
  }).mount('#app');
</script>
Enter fullscreen mode Exit fullscreen mode
  • {{ message }}: This is Vue’s interpolation syntax for binding data to the DOM.
  • @click: This is a shorthand for the v-on:click directive, which listens for click events.
  • data(): A function that returns the initial state of the app.
  • methods: An object containing functions that can be called from the template.

Two-Way Data Binding with v-model

Vue makes it easy to bind form inputs to data using the v-model directive.

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

<script>
  const { createApp } = Vue;

  createApp({
    data() {
      return {
        name: ""
      };
    }
  }).mount('#app');
</script>
Enter fullscreen mode Exit fullscreen mode
  • v-model: Creates a two-way binding between the input field and the name property.
  • As you type in the input field, the name property updates, and the message below reflects the change.

Creating Reusable Components

<div id="app">
  <greeting-component></greeting-component>
</div>

<script>
  const { createApp } = Vue;

  const GreetingComponent = {
    template: `<p>Welcome to Vue.js!</p>`
  };

  createApp({
    components: {
      GreetingComponent
    }
  }).mount('#app');
</script>
Enter fullscreen mode Exit fullscreen mode
  • GreetingComponent: A simple component that renders a welcome message.
  • components: Registers the component locally within the app.

Why Choose Vue.js?

  1. Easy to Learn: Vue’s syntax is intuitive and easy to pick up, especially if you’re familiar with HTML, CSS, and JavaScript.

  2. Flexible: You can use Vue for small projects or scale it up for large applications.

  3. Performance: Vue’s reactivity system ensures efficient updates to the DOM.

  4. Ecosystem: Vue has a rich ecosystem, including tools like Vue Router for routing and Vuex for state management.

Conclusion

Vue.js is a powerful and beginner-friendly framework that makes building modern web applications a breeze. Whether you’re creating a simple interactive UI or a complex SPA, Vue’s reactivity and component-based architecture will help you get the job done efficiently.

sources

https://vuejs.org/guide/introduction.html
https://vuejs.org/tutorial/#step-1
https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Frameworks_libraries/Vue_getting_started
https://vuejs.org/guide/quick-start

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please show some love ❤️ or share a kind word in the comments if you found this useful!

Got it!