Skip to main content
FREE Mock interview & resume review with Sumit. Free right now, no payment needed.Book a slot

Introduction to Vue.js

Learn Vue.js - progressive JavaScript framework for building user interfaces. Master Vue.js components, reactivity, and composition API.

jiocoder TeamUpdated Jun 6, 20263 min readVue.js
Introduction to Vue.js

What is Vue.js?#

Vue.js is a progressive JavaScript framework for building user interfaces. Created by Evan You, Vue is designed to be incrementally adoptable - you can use as much or as little of Vue as you need. It's known for its gentle learning curve, excellent documentation, and reactive data binding.

Vue.js combines the best features of React and Angular, offering a template-based syntax similar to Angular but with the component-based architecture of React. It's lightweight, performant, and has a vibrant ecosystem.

Installation#

Install Vue.js using npm or yarn:

Code
# Using npm
npm install vue@next

# Using yarn
yarn add vue@next

# Using CDN
<script src="https://unpkg.com/vue@next"></script>

Create a Vue project with Vue CLI:

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

# Create new project
vue create my-vue-app

# Or use Vite (faster)
npm create vue@latest my-vue-app

Basic Component#

Vue components use a template, script, and style structure:

Code
<template>
  <div class="counter">
    <h1>{{ message }}</h1>
    <button @click="increment">Count: {{ count }}</button>
    <button @click="decrement">Decrement</button>
    <button @click="reset">Reset</button>
  </div>
</template>

<script>
export default {
  name: 'Counter',
  data() {
    return {
      message: 'Hello Vue!',
      count: 0
    };
  },
  methods: {
    increment() {
      this.count++;
    },
    decrement() {
      this.count--;
    },
    reset() {
      this.count = 0;
    }
  },
  computed: {
    doubleCount() {
      return this.count * 2;
    }
  },
  watch: {
    count(newValue, oldValue) {
      console.log(`Count changed from ${oldValue} to ${newValue}`);
    }
  }
};
</script>

<style scoped>
.counter {
  padding: 20px;
}
button {
  margin: 5px;
  padding: 10px 20px;
}
</style>

Composition API#

Vue 3 introduces the Composition API for better code organization:

Code
<template>
  <div>
    <h1>{{ title }}</h1>
    <p>Count: {{ count }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue';

const title = ref('Vue 3 App');
const count = ref(0);

const doubleCount = computed(() => count.value * 2);

watch(count, (newValue, oldValue) => {
  console.log(`Count: ${oldValue} → ${newValue}`);
});

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

Directives#

Code
<template>
  <div>
    <!-- v-if / v-else -->
    <p v-if="isVisible">Visible</p>
    <p v-else>Hidden</p>
    
    <!-- v-show -->
    <p v-show="isVisible">Toggle visibility</p>
    
    <!-- v-for -->
    <ul>
      <li v-for="item in items" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
    
    <!-- v-bind (shorthand : ) -->
    <img :src="imageUrl" :alt="imageAlt">
    
    <!-- v-on (shorthand @ ) -->
    <button @click="handleClick" @mouseover="handleHover">Click</button>
    
    <!-- v-model -->
    <input v-model="inputValue" placeholder="Enter text">
  </div>
</template>

Props and Events#

Code
// Child Component
<template>
  <div>
    <h2>{{ title }}</h2>
    <button @click="$emit('increment')">Increment</button>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      required: true,
      default: 'Default Title'
    }
  },
  emits: ['increment']
};
</script>

// Parent Component
<template>
  <ChildComponent 
    :title="pageTitle" 
    @increment="handleIncrement" 
  />
</template>

Vue Router#

Code
// Install
npm install vue-router@4

// router/index.js
import { createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue';

const routes = [
  { path: '/', component: Home },
  { path: '/about', component: () => import('../views/About.vue') },
  { path: '/user/:id', component: () => import('../views/User.vue') }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

export default router;

State Management with Pinia#

Code
// Install
npm install pinia

// stores/counter.js
import { defineStore } from 'pinia';

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0
  }),
  getters: {
    doubleCount: (state) => state.count * 2
  },
  actions: {
    increment() {
      this.count++;
    }
  }
});

// Usage in component
import { useCounterStore } from '@/stores/counter';

const counter = useCounterStore();
counter.increment();
console.log(counter.count);

Best Practices#

  • Use Composition API for new projects
  • Keep components small and focused
  • Use computed properties for derived state
  • Leverage Vue's reactivity system
  • Use v-show for frequent toggles, v-if for conditional rendering
  • Always provide keys in v-for loops
  • Use scoped styles to avoid CSS conflicts

Vue.js is an excellent choice for building modern web applications. Its progressive nature means you can start simple and add complexity as needed, making it perfect for both beginners and experienced developers.

Article discussion

(0)

Join the discussion — login to comment and help other developers.

Login to comment