<template> <button @click="handleClick">Click me</button> </template> <script setup> import { ref } from 'vue' const count = ref(0) function handleClick() { count.value++ console.log('Method count:', count.value) } </script>
The method handler increments the reactive count variable and logs the updated value. So each click increases the count and logs the new number.
<template> <button @click="???">Click me</button> </template> <script setup> import { ref } from 'vue' const count = ref(0) </script>
count is a ref and you must access its value.Since count is a ref, you must use count.value++ to update it inline. Using count++ will not update the reactive value.
<template> <button @click="count++">Click me</button> <p>Count: {{ count }}</p> </template> <script setup> import { ref } from 'vue' const count = ref(0) </script>
count is a ref object. Using count++ tries to increment the object itself, which does nothing. You must increment count.value to update the reactive value and the UI.
Inline handlers create a new function every render, which can cause unnecessary re-renders or memory use. Method handlers reuse the same function reference, improving performance.
count after clicking the 'Click me' button twice?<template> <button @click="increment">Click me</button> <button @click="count.value++">Inline Increment</button> <p>Count: {{ count }}</p> </template> <script setup> import { ref } from 'vue' const count = ref(0) function increment() { count.value += 2 } </script>
increment method adds 2 per click.Clicking the first button calls increment() which adds 2. Clicking the second button adds 1. Two clicks total means 2 + 2 = 4 if clicking the first button twice, or mixed clicks add accordingly.