index.jsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { defineComponent, ref, watch, onMounted, computed } from 'vue'
  2. import { CountUp } from 'countup.js'
  3. import { componentScheme } from './component.scheme'
  4. import { generateProps } from '../helper/component-util'
  5. export default defineComponent({
  6. name: 'CipCountUp',
  7. props: generateProps(componentScheme),
  8. setup (props) {
  9. const countRef = ref()
  10. const value = computed(() => {
  11. return props.endVal || props.modelValue
  12. })
  13. const countUp = ref()
  14. const newCountUP = () => {
  15. return new CountUp(countRef.value, props.endVal || props.modelValue, {
  16. duration: props.duration,
  17. startVal: props.startVal,
  18. decimalPlaces: props.decimalPlaces,
  19. prefix: props.prefix,
  20. suffix: props.suffix
  21. })
  22. }
  23. onMounted(() => {
  24. countUp.value = newCountUP()
  25. countUp.value.start()
  26. watch(props, () => {
  27. // 监听动态配置项,组件本身不支持动态配置,需要重新创建实例
  28. countUp.value = newCountUP()
  29. countUp.value.update(value.value)
  30. })
  31. })
  32. return () => (
  33. <span ref={countRef} />
  34. )
  35. }
  36. })