Method handlers vs inline handlers in Vue
📖 Scenario: You are building a simple Vue app that shows a button to greet the user. You want to learn how to handle button clicks using two ways: method handlers and inline handlers.
🎯 Goal: Create a Vue component that has a button. When clicked, it shows an alert with a greeting message. First, use a method handler. Then, add an inline handler that shows a different alert message.
📋 What You'll Learn
Create a Vue component using the
<script setup> syntaxAdd a method called
sayHello that shows an alert with the message 'Hello from method!'Add a button that uses
@click="sayHello" to call the method handlerAdd a second button that uses an inline handler
@click="alert('Hello from inline!')"Use semantic HTML and accessible button elements
💡 Why This Matters
🌍 Real World
Buttons with click handlers are common in web apps for user interactions like submitting forms or showing messages.
💼 Career
Understanding method vs inline handlers is essential for writing clean, maintainable Vue components in professional frontend development.
Progress0 / 4 steps