📖 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> syntax
Add 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 handler
Add 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
1
Create the Vue component and method handler
Create a Vue component with <script setup>. Inside it, define a method called sayHello that shows an alert with the message 'Hello from method!'.
Vue
Hint
Use function sayHello() { alert('Hello from method!') } inside <script setup>.
2
Add a button with method handler
Inside the <template>, add a button element with the text Greet with Method. Use @click="sayHello" on the button to call the method handler when clicked.
Vue
Hint
Use <button @click="sayHello">Greet with Method</button> inside the template.
3
Add a button with inline handler
Add a second button inside the <template> with the text Greet Inline. Use an inline click handler @click="alert('Hello from inline!')" on this button.
Vue
Hint
Add <button @click="alert('Hello from inline!')">Greet Inline</button> inside the template.
4
Add accessibility and finalize component
Add aria-label attributes to both buttons for accessibility. Use aria-label="Greet with method button" on the first button and aria-label="Greet inline button" on the second button.
Vue
Hint
Add aria-label attributes inside the <button> tags for accessibility.
Practice
(1/5)
1. In Vue, what is the main advantage of using a method handler over an inline handler in a template?
easy
A. Methods improve code clarity and allow reuse across the component.
B. Methods run faster than inline handlers.
C. Methods can only be used for event listeners, not bindings.
D. Methods automatically update the DOM without reactivity.
Solution
Step 1: Understand method handlers in Vue
Method handlers are functions defined in the component that can be called from the template. They help organize code and can be reused multiple times.
Step 2: Compare with inline handlers
Inline handlers run simple expressions directly in the template but can become messy if complex logic is needed. Methods keep the template clean and code maintainable.
Final Answer:
Methods improve code clarity and allow reuse across the component. -> Option A
Believing methods auto-update DOM without reactivity
Confusing methods with inline expressions for simple tasks
2. Which of the following is the correct syntax to call a method named submitForm on a button click in Vue?
easy
A. Submit
B. Submit
C. Submit
D. Submit
Solution
Step 1: Understand method reference syntax in Vue templates
To bind a method to an event handler, reference the method name without parentheses. Vue automatically calls it with the event object when the event fires.
Step 2: Analyze each option
<button @click="submitForm">Submit</button> correctly references the method (Submit). <button @click="submitForm()">Submit</button> calls it immediately on render (Submit). <button v-on:click="submitForm()">Submit</button> also calls immediately (Submit). <button @click="submitForm.call()">Submit</button> uses invalid syntax (Submit).
Final Answer:
<button @click="submitForm">Submit</button> -> Option A
Quick Check:
Method reference = no parentheses () [OK]
Hint: Event handlers reference methods WITHOUT () [OK]
Common Mistakes:
Adding parentheses, causing immediate execution on render
Using incorrect .call() syntax
Confusing method reference (no ()) with immediate call (with ())