Skip to content

17 - useCounter #2862

Open
Open
@geek4ctrl

Description

@geek4ctrl
// your answers
<script setup lang='ts'> import { ref } from 'vue'; interface UseCounterOptions { min?: number max?: number } /** * Implement the composable function * Make sure the function works correctly */ function useCounter(initialValue = 0, options: UseCounterOptions = {}) { const count = ref(initialValue); function inc(): number { count.value < options.max ? count.value++ : count.value return count.value; } function dec(): number { count.value > options.min ? count.value-- : count.value return count.value; } function reset(): number { count.value = 0; return count.value; } return { count, inc, dec, reset} } const { count, inc, dec, reset } = useCounter(0, { min: 0, max: 10 }) </script>

Count: {{ count }}

inc dec reset

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions