Overview
TheCountPageComponent is the main feature component that displays an interactive counter. Users can increment, decrement, and reset the counter value with a minimum value constraint of 0.
Component Definition
Component Configuration
External template file containing the component’s HTML structure
Component-scoped stylesheet for custom button styling
Properties
counter
10 by default and constrained to a minimum value of 0.
Methods
incrementby(value: number)
Increases or decreases the counter by the specified value while ensuring it never goes below zero.The amount to add to the counter. Can be positive (increment) or negative (decrement)
- Uses
Math.max(0, ...)to ensure the counter never goes below 0 - Accepts negative values for decrementing
- Accepts positive values for incrementing
The counter has a built-in constraint that prevents negative values. Attempting to decrement below 0 will result in the counter staying at 0.
reset()
Resets the counter back to zero.Template
The component template provides an interactive UI for the counter:Template Features
- Data Binding: Uses interpolation
{{counter}}to display the current value - Event Binding: Buttons use
(click)event binding to trigger component methods - Increment Button: Calls
incrementby(1)to increase the counter - Decrement Button: Calls
incrementby(-1)to decrease the counter - Reset Button: Calls
reset()to return counter to 0
Styling
The component includes custom CSS for button styling:Button Styles
- Background: Bright green-yellow color (
greenyellow) - Padding: 5px vertical, 20px horizontal
- Border: No border with 5px rounded corners
- Spacing: 20px top/bottom margin, 10px left/right margin
- Cursor: Pointer on hover for better UX
Usage Example
Key Features
- Zero-Constrained Counter: Automatically prevents negative values
- Flexible Increment: Single method handles both increment and decrement
- Simple State Management: Uses plain class properties (no complex state libraries needed)
- Component-Scoped Styles: CSS is encapsulated to this component
- External Templates: Separates HTML and CSS for better maintainability
Best Practices
Related
- App Component - Root application component
- Routing Configuration - Setting up routes