-
-
Notifications
You must be signed in to change notification settings - Fork 202
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 = {}) {
const count = ref<number>(initialValue)
const { min, max } = options
const inc = () =>{
const curCount = count.value
if (curCount < max || !max) {
count.value ++
}
}
const dec = () =>{
const curCount = count.value
if (curCount > min || (min !== 0 && !min) ) {
count.value --
}
}
const reset = () => {
count.value = initialValue
}
return {
count,
inc,
dec,
reset
}
}
const { count, inc, dec, reset } = useCounter(0)
</script>
<template>
<p>Count: {{ count }}</p>
<button @click="inc">
inc
</button>
<button @click="dec">
dec
</button>
<button @click="reset">
reset
</button>
</template>