Method handlers and inline handlers let you respond to user actions in Vue. They help your app do things when buttons are clicked or keys pressed.
Method handlers vs inline handlers in Vue
<button @click="methodName">Click me</button> <button @click="inlineExpression">Click me</button>
Method handlers call a function defined in the methods section of your Vue component.
Inline handlers run simple JavaScript expressions directly inside the template.
<template> <button @click="sayHello">Say Hello</button> </template> <script setup> function sayHello() { alert('Hello!') } </script>
<template> <button @click="count++">Increase Count</button> <p>Count: {{ count }}</p> </template> <script setup> import { ref } from 'vue' const count = ref(0) </script>
<template> <button @click="greet('Vue')">Greet Vue</button> </template> <script setup> function greet(name) { alert(`Hello, ${name}!`) } </script>
<template> <button @click="count = count + 1">Add One</button> <p>Count: {{ count }}</p> </template> <script setup> import { ref } from 'vue' const count = ref(0) </script>
This Vue component shows two buttons. One uses a method handler to increase the count. The other uses an inline handler. Both update the same count displayed below.
<template> <button @click="increment">Increment Method</button> <button @click="count++">Increment Inline</button> <p>Count: {{ count }}</p> </template> <script setup> import { ref } from 'vue' const count = ref(0) function increment() { count.value++ } </script>
Use method handlers for complex logic or when you want to reuse code.
Use inline handlers for simple, quick actions to keep your code short.
Too much logic in inline handlers can make templates hard to read.
Method handlers call functions defined in your Vue component.
Inline handlers run simple expressions directly in the template.
Choose method handlers for clarity and reuse; inline handlers for quick, simple actions.
Practice
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 AQuick Check:
Method handlers = clarity and reuse [OK]
- Thinking methods run faster than inline handlers
- Believing methods auto-update DOM without reactivity
- Confusing methods with inline expressions for simple tasks
submitForm on a button click in Vue?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 AQuick Check:
Method reference = no parentheses () [OK]
- Adding parentheses, causing immediate execution on render
- Using incorrect .call() syntax
- Confusing method reference (no ()) with immediate call (with ())
<template>
<button @click="count++">Increment</button>
<p>Count: {{ count }}</p>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>What happens when the button is clicked?
Solution
Step 1: Understand inline handler behavior
The inline handler@click="count++"directly increments the reactivecountvariable when clicked.Step 2: Check reactivity and display update
Sincecountis a ref, Vue tracks changes and updates the displayed value in{{ count }}automatically.Final Answer:
The count increases by 1 and updates the displayed number. -> Option BQuick Check:
Inline handler increments reactive count [OK]
- Assuming inline handlers cannot modify refs
- Thinking count++ causes errors in templates
- Believing count resets on each click
<button @click="increment">Add</button>
<script setup>
import { ref } from 'vue'
const count = ref(0)
function increment() {
count++
}
</script>Solution
Step 1: Check how to update a ref in Vue
A ref's value must be updated via its.valueproperty, not directly withcount++.Step 2: Analyze the method increment()
The method tries to docount++, which is invalid becausecountis a ref object, not a number.Step 3: Note the event handler syntax
The @click="increment" is valid because Vue calls the method on click, so no error there.Final Answer:
You cannot increment a ref directly with count++ inside the method. -> Option DQuick Check:
Update ref with count.value++ [OK]
- Trying to increment ref directly without .value
- Adding unnecessary prefixes to method names
- Confusing method call syntax in templates
isActive in your Vue component when a button is clicked. Which approach best balances clarity and simplicity?Solution
Step 1: Consider clarity and reuse
Using a method handler keeps the template clean and allows reuse if toggling is needed elsewhere.Step 2: Compare inline handler
Inline handler is simple but can clutter the template if logic grows or is reused.Step 3: Evaluate other options
Using both handlers together is redundant and confusing. Computed properties are for derived state, not event handling.Final Answer:
Use a method handler: define toggleActive() method and call it with @click="toggleActive()". -> Option CQuick Check:
Method handlers = clarity + reuse [OK]
- Using inline handlers for complex or reusable logic
- Trying to toggle state in computed properties
- Combining inline and method handlers unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
