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):```shnpm create vite@latest my-vue-app --template vuecd my-vue-appnpm installnpm run dev
πΉ Vue.js Syntax & Basics
- 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.
- Vue Directives
Directives are special attributes prefixed with v-
, used to manipulate the DOM dynamically.
v-bind
(Bind Attribute)
<img v-bind:src="imageUrl" />
Shortcut:
<img :src="imageUrl" />
v-if
,v-else
, andv-show
(Conditional Rendering)
<p v-if="isLoggedIn">Welcome, User!</p><p v-else>Please log in.</p>
v-for
(List Rendering | Looping Over Lists)
<ul><li v-for="item in items" :key="item.id">{{ item.name }}</li></ul>
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.
- Creating a Component
<script>Vue.component("greeting-message", { template: "<h2>Hello from Vue Component!</h2>"});</script>
- Using a Component
<greeting-message></greeting-message>
πΉ Vue.js Props & Events
- Props (Passing Data to Components)
<user-card name="Alice"></user-card>
<script>Vue.component("user-card", { props: ["name"], template: "<h3>User: {{ name }}</h3>"});</script>
- 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
- 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.
- 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:
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:
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
Feature | Vue.js | React | Angular |
---|---|---|---|
Type | Framework | Library | Framework |
Learning Curve | Moderate | Easy | Steep |
State Management | Vuex, Pinia | Hooks (useState , useReducer ) | Services & RxJS |
Performance | Fast (Reactivity System) | Fast (Virtual DOM) | Moderate |
Best For | UI Components & Apps | UI Components | Enterprise 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!