Posts

Showing posts with the label component

Emitting Events in Vue.js — Communicating from Child to Parent

Image
Emitting Events in Vue.js In Vue.js, emitting events is how a child component sends information back to its parent . It’s the opposite of props and allows components to communicate clearly. Why Emit Events? To notify parent components about user actions To keep logic and UI separate and clean Example: Emitting an Event // Parent Template <child-button @sayHello="handleHello" /> <script> app.component('child-button', { template: `<button @click="$emit('sayHello')">Click Me</button>` }); const app = Vue.createApp({ methods: { handleHello() { alert("Hello from the child component!"); } } }); app.mount('#app'); </script> Explanation: The child emits a custom event sayHello , and the parent listens to it using @sayHello . When clicked, the alert is triggered in the parent. Best Practices: Use clear and descriptive event names Keep...

Beginner’s Guide to Creating Your First Vue Component

Image
Beginner’s Guide to Creating Your First Vue Component Intro: If you're starting your Vue.js journey, creating your first component is a huge milestone. Components are the building blocks of every Vue app—and the good news? They’re super easy to get started with! In this post, you’ll learn how to create a basic Vue component step-by-step. 1. What is a Vue Component? A Vue component is a reusable piece of UI that you can use across your app. It can be as simple as a button or as complex as an entire form. Think of components like LEGO blocks—small parts that come together to build something awesome. 2. Setting Up Your First Vue Project (Quick Option) You can test components online at Vue SFC Playground or install Vue locally using Vue CLI: npm install -g @vue/cli vue create my-first-app cd my-first-app npm run serve 3. Creating a Simple Vue Component Let’s create a simple component named HelloWorld.vue : <template> <div> <h2>{{ message ...