Vue Counter (Guarded Decrement)

easy
By FrontendAtlas Editorial · Updated Nov 12, 2026
easy level
Vue 3 Composition API (ref, computed)Basic component structureClick event handlersGuarded state updates
Build a Vue 3 counter component with increment/decrement and a zero floor. Use reactive state and guard the decrement so it never goes negative. Call out disabled states and clean template logic. Concepts: vue, components, reactivity, event handlers, basics. Framework focus: Vue SFC patterns, ref/reactive state, computed/watch, and v-model bindings.

Requirements

  • Render the counter UI as the main component.
  • Display the current count value.
  • Provide '+' and '−' buttons to increment and decrement.
  • Prevent the count from ever going below 0.
  • Include a 'Reset' button that sets the count back to 0.

What the checks look for

The checks find elements by .value, button "-", button "+", button "Reset". Keep these in your markup.

Expected behavior

  • Initial count starts at 0.
  • Clicking '+' increases the count by 1.
  • Clicking '−' decreases the count by 1, but never below 0.
  • Clicking 'Reset' sets the count back to 0.
  • The '−' button is disabled when the count is 0.

Implementation notes

  • Use Vue 3's Composition API with ref to store the count.
  • Use a computed property to determine when the count is zero and disable the decrement button.
  • Keep everything in a single <App.vue> component for this exercise.

Solution idea

Use Vue 3's Composition API with ref(0) for the count and a computed property that returns true when the count is 0. The '+' button increments the ref, the '−' button decrements but is guarded so the value never becomes negative, and 'Reset' sets it back to 0.