Open
Description
<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 = {}) {
return {
count: ref<number>(initialValue),
inc() { count.value < options.max && count.value++ },
dec() { count.value > options.min && count.value-- },
reset() { count.value = initialValue }
}
}
const { count, inc, dec, reset } = useCounter(5, { min: 5, max: 10 })
</script>
<template>
<p>Count: {{ count }}</p>
<button @click="inc">
inc
</button>
<button @click="dec">
dec
</button>
<button @click="reset">
reset
</button>
</template>