Skip to content

Vue.js Basics | A Beginner’s Guide

πŸ”Ή What is Vue.js?

Vue.js is a progressive JavaScript framework used for building modern, interactive web applications. Developed by Evan You, Vue is known for its simplicity, reactivity, and flexibility.

βœ… Component-Based – Breaks UI into reusable components.
βœ… Reactive Data Binding – Automatically updates the UI when data changes.
βœ… Lightweight & Fast – Minimal footprint compared to other frameworks.
βœ… Easy to Learn – Simple syntax with a gentle learning curve.


πŸ”Ή Installing Vue.js

To install Vue.js via CDN, add the following script to your HTML file:

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
To set up Vue with Vite (recommended for modern apps):
```sh
npm create vite@latest my-vue-app --template vue
cd my-vue-app
npm install
npm run dev

πŸ”Ή Vue.js Syntax & Basics

  1. Creating a Vue App
<div id="app">
<h1>{{ message }}</h1>
</div>
<script>
const app = Vue.createApp({
data() {
return {
message: "Hello, Vue.js!"
};
}
});
app.mount("#app");
</script>

βœ… The {{ message }} syntax is Vue’s template syntax, allowing data binding. βœ… Vue.createApp({}) initializes a Vue instance. βœ… app.mount(β€œ#app”) attaches Vue to the HTML element.

  1. Vue Directives

Directives are special attributes prefixed with v-, used to manipulate the DOM dynamically.

  1. v-bind (Bind Attribute)
<img v-bind:src="imageUrl" />

Shortcut:

<img :src="imageUrl" />
  1. v-if, v-else, and v-show (Conditional Rendering)
<p v-if="isLoggedIn">Welcome, User!</p>
<p v-else>Please log in.</p>
  1. v-for (List Rendering | Looping Over Lists)
<ul>
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>
  1. v-on (Two-Way Data Binding)
<input v-model="username" />
<p>Entered Name: {{ username }}</p>

βœ… Syncs input fields with Vue data.


πŸ”Ή Vue.js Components

Vue components are reusable UI elements.

  1. Creating a Component
<script>
Vue.component("greeting-message", {
template: "<h2>Hello from Vue Component!</h2>"
});
</script>
  1. Using a Component
<greeting-message></greeting-message>

πŸ”Ή Vue.js Props & Events

  1. Props (Passing Data to Components)
<user-card name="Alice"></user-card>
<script>
Vue.component("user-card", {
props: ["name"],
template: "<h3>User: {{ name }}</h3>"
});
</script>
  1. Events ($emit)
<child-component @send-message="handleMessage"></child-component>
<script>
Vue.component("child-component", {
template: `<button @click="$emit('send-message', 'Hello from child!')">Click Me</button>`
});
const app = Vue.createApp({
methods: {
handleMessage(msg) {
alert(msg);
}
}
});
app.mount("#app");
</script>

βœ… $emit sends an event to the parent component. βœ… The parent listens for the event using @eventName=β€œmethod”.


πŸ”Ή Vue Computed Properties & Watchers

  1. Computed Properties (Optimized Data Processing)
<p>Full Name: {{ fullName }}</p>
<script>
const app = Vue.createApp({
data() {
return {
firstName: "John",
lastName: "Doe"
};
},
computed: {
fullName() {
return this.firstName + " " + this.lastName;
}
}
});
</script>

βœ… Computed properties cache results for better performance.

  1. Watchers (Asynchronous Data Handling)
<p>Search Query: {{ searchQuery }}</p>
<script>
const app = Vue.createApp({
data() {
return { searchQuery: "" };
},
watch: {
searchQuery(newVal) {
console.log("Search changed to:", newVal);
}
}
});
</script>

βœ… Watchers track changes in data and trigger functions accordingly.

πŸ”Ή Vue Router (Navigation) Install Vue Router:

Terminal window
npm install vue-router

Setting Up Vue Router

import { createRouter, createWebHistory } from "vue-router";
import Home from "./components/Home.vue";
import About from "./components/About.vue";
const routes = [
{ path: "/", component: Home },
{ path: "/about", component: About }
];
const router = createRouter({
history: createWebHistory(),
routes
});
export default router;

πŸ”Ή Vuex (State Management)

Install Vuex:

Terminal window
npm install vuex

Setting Up Vuex Store

import { createStore } from "vuex";
const store = createStore({
state() {
return { count: 0 };
},
mutations: {
increment(state) {
state.count++;
}
}
});
export default store;

βœ… Use Vuex for centralized state management in large applications.

πŸ”Ή Vue vs Other Frontend Frameworks

FeatureVue.jsReactAngular
TypeFrameworkLibraryFramework
Learning CurveModerateEasySteep
State ManagementVuex, PiniaHooks (useState, useReducer)Services & RxJS
PerformanceFast (Reactivity System)Fast (Virtual DOM)Moderate
Best ForUI Components & AppsUI ComponentsEnterprise Apps

πŸ”Ή Getting Started with Vue Development 1️⃣ Install Node.js β†’ Download from Node.js Official Site
2️⃣ Create a Vue App β†’ npm create vite@latest my-vue-app β€”template vue
3️⃣ Start Development Server β†’ npm run dev
4️⃣ Build for Production β†’ npm run build


πŸ”Ή Conclusion

Vue.js is a lightweight, flexible, and reactive framework perfect for modern web applications. Whether you’re a beginner or an experienced developer, mastering Vue’s directives, components, state management, and Vue Router will help you build powerful, scalable applications.

πŸš€ Next Steps? Explore Vue 3 Composition API, Vue Router, and Pinia for state management!