Interactive Counter with Event Handling in Vue
This challenge focuses on implementing basic event handling in a Vue.js application using TypeScript. You'll build a simple counter component that increments and decrements a value based on button clicks, demonstrating how to bind methods to events and update data reactively. This is a fundamental skill for building interactive Vue applications.
Problem Description
You are tasked with creating a Vue component that displays a counter value and provides two buttons: "Increment" and "Decrement." When the "Increment" button is clicked, the counter value should increase by 1. When the "Decrement" button is clicked, the counter value should decrease by 1. The component should also display a message indicating whether the counter is positive, negative, or zero.
Key Requirements:
- Reactive Data: The counter value should be a reactive data property in your Vue component.
- Event Binding: You must use Vue's event binding (
@click) to connect the buttons to methods that update the counter value. - Method Definition: Create methods to handle the increment and decrement actions. These methods should update the reactive data property.
- Conditional Display: Display a message based on the counter's value:
- "Positive" if the counter is greater than 0.
- "Negative" if the counter is less than 0.
- "Zero" if the counter is equal to 0.
- TypeScript: The code must be written in TypeScript, including proper type annotations for data properties and method parameters.
Expected Behavior:
- The component should initially render with a counter value of 0 and the message "Zero."
- Clicking the "Increment" button should increase the counter value by 1 and update the message accordingly.
- Clicking the "Decrement" button should decrease the counter value by 1 and update the message accordingly.
- The UI should update immediately after each button click.
Edge Cases to Consider:
- The counter value should not go below a minimum value (e.g., -100) or above a maximum value (e.g., 100). (This is optional, but good practice).
- Ensure the message updates correctly when the counter reaches 0 from either positive or negative values.
Examples
Example 1:
Initial State: counter = 0, message = "Zero"
Input: User clicks "Increment" button
Output: counter = 1, message = "Positive"
Explanation: The increment method is called, increasing the counter by 1. The conditional display updates the message to "Positive."
Example 2:
Initial State: counter = 5, message = "Positive"
Input: User clicks "Decrement" button
Output: counter = 4, message = "Positive"
Explanation: The decrement method is called, decreasing the counter by 1. The conditional display remains "Positive" because the counter is still greater than 0.
Example 3:
Initial State: counter = -2, message = "Negative"
Input: User clicks "Increment" button multiple times until counter = 0
Output: counter = 0, message = "Zero"
Explanation: The increment method is called repeatedly, increasing the counter until it reaches 0. The conditional display updates the message to "Zero."
Constraints
- Component Structure: You should create a single Vue component for this challenge.
- Data Type: The counter value must be a number.
- Event Handling: Use Vue's
@clickdirective for event binding. - TypeScript: All code must be valid TypeScript.
- No External Libraries: Do not use any external libraries beyond Vue and TypeScript.
Notes
- Think about how to structure your Vue component (e.g., using
template,data,methods). - Remember that Vue's reactivity system automatically updates the DOM when data properties change.
- Consider using template expressions to display the counter value and the message.
- You can use a simple
setup()function for your component. - Focus on clear and concise code that adheres to Vue's best practices.