Google:

adverisment

Vue.js tutorial

details img

Introduction to Vue.js

Vue.js is a progressive JavaScript framework for building user interfaces. It’s known for its simplicity, flexibility, and ease of integration with existing projects.

Key Features

  • Reactive Data Binding: Automatically updates the DOM when data changes.
  • Component-Based Architecture: Encapsulates UI logic into reusable components.
  • Directives: Simplifies DOM manipulation.
  • Vue CLI: Streamlines project setup.

Setting Up Vue.js

1. Installation

Option 1: Use a CDN (For quick testing)

Include the Vue.js library in your HTML:

html
<script src="https://cdn.jsdelivr.net/npm/vue@3"></script>

Option 2: Install with npm (Recommended for projects)

bash
npm install vue@next

Option 3: Use Vue CLI (For full-featured projects)

Install Vue CLI globally:

bash
npm install -g @vue/cli

Create a new Vue project:

bash
vue create my-vue-app

Navigate to the project folder:

bash
cd my-vue-app npm run serve

Basic Vue.js Application

Here’s a simple Vue.js app to understand the basics:

1. Basic HTML Setup

html
<div id="app"> <h1>{{ message }}</h1> <button @click="reverseMessage">Reverse Message</button> </div> <script src="https://cdn.jsdelivr.net/npm/vue@3"></script> <script> const app = Vue.createApp({ data() { return { message: "Hello, Vue.js!" }; }, methods: { reverseMessage() { this.message = this.message.split('').reverse().join(''); } } }); app.mount('#app'); </script>

Core Concepts

1. Reactive Data

Vue’s data() function provides reactive properties:

javascript
data() { return { count: 0 }; }

2. Directives

  • v-bind: Dynamically bind attributes.
    html
    <a v-bind:href="url">Visit</a>
  • v-if and v-else Conditional rendering.
    html
    <p v-if="isLoggedIn">Welcome back!</p> <p v-else>Please log in.</p>
  • v-for Loop through arrays or objects.
    html
    <ul> <li v-for="item in items" :key="item.id">{{ item.name }}</li> </ul>

3. Event Handling

Use v-on or @ to listen for events:

html
<button @click="increment">Click Me</button> <script> methods: { increment() { this.count++; } } </script>

4. Two-Way Binding

Use v-model for form inputs:

html
<input v-model="name" /> <p>Hello, {{ name }}!</p>

Components

Vue apps are built with reusable components.

Example Component

1. Define a Component:

javascript
app.component('greeting-message', { props: ['name'], template: '<h2>Hello, {{ name }}!</h2>' });

2. Use the Component:

html
<greeting-message name="Alice"></greeting-message>

Vue CLI Structure

When using Vue CLI, the structure looks like this:

bash
src/ main.js # Entry file App.vue # Root component components/ # Reusable components

Example (App.vue):

html
<template> <div> <h1>Welcome to Vue.js!</h1> <counter></counter> </div> </template> <script> import Counter from './components/Counter.vue'; export default { components: { Counter } }; </script>

State Management with Vuex

For managing global state, use Vuex (official state management library).

bash
npm install vuex@next

Deploying Vue.js Apps

  • Use npm run build to bundle the app for production.
  • Deploy the output dist folder to any web server or hosting platform (e.g., Netlify, Vercel).

Leave Comment