Vue CountUp v3
vue-countup-v3 is a Vue 3 component that wraps the popular countUp.js library (specifically version 2.6.2), extending its functionality with additional features. It provides an animated counting experience for numerical values within Vue applications. The package is currently at version 1.4.2, indicating active development with recent minor updates. It serves as a convenient and optimized way to integrate dynamic number animations into Vue 3 projects without directly managing the countUp.js instance. Key differentiators include its native Vue 3 component interface, built-in reactivity, and support for Vue-specific patterns like slots for prefix/suffix customization.
Common errors
-
Module 'vue-countup-v3' could not be resolved in Nuxt 3 build or similar build-time errors.
cause Nuxt 3's build process isn't automatically transpiling the 'vue-countup-v3' package.fixAdd 'vue-countup-v3' to the `transpile` array in your `nuxt.config.ts`: `export default defineNuxtConfig({ build: { transpile: ['vue-countup-v3'] } });` -
Hydration mismatch error or component not rendering on the client-side in Astro.
cause Astro requires explicit client-side rendering directives for interactive UI frameworks.fixAdd `client:load client:only` directives to your `CountUp` component within Astro: `<CountUp client:load client:only :end-val="2000" />` -
TS2724: 'ICountUp' is not a runtime value and cannot be used in 'import' type checking. (or similar for 'CountUpOptions')
cause Attempting to import TypeScript types using a standard runtime 'import' statement, which leads to compilation errors or unexpected bundling behavior.fixChange your type imports from `import { ICountUp, CountUpOptions } from 'vue-countup-v3'` to `import type { ICountUp, CountUpOptions } from 'vue-countup-v3'`.
Warnings
- gotcha When integrating `vue-countup-v3` into a Nuxt 3 project, you must explicitly configure Nuxt to transpile the package. Without this, you might encounter build errors or issues with client-side hydration.
- gotcha In Astro projects, `vue-countup-v3` is a client-side component and requires explicit client-side rendering directives to function correctly. Without these, the component will not render or hydrate properly.
- gotcha The component wraps `countUp.js`, and most direct `countUp.js` configuration options should be passed via the `:options` prop as an object. Avoid trying to pass these directly as root component props unless they are explicitly documented as such by `vue-countup-v3`.
- gotcha The `delay` prop specifically controls the interval *between loops* when the `loop` prop is enabled. It does not introduce an initial delay before the very first animation begins.
Install
-
npm install vue-countup-v3 -
yarn add vue-countup-v3 -
pnpm add vue-countup-v3
Imports
- CountUp
const CountUp = require('vue-countup-v3')import CountUp from 'vue-countup-v3'
- ICountUp
import { ICountUp } from 'vue-countup-v3'import type { ICountUp } from 'vue-countup-v3' - CountUpOptions
import { CountUpOptions } from 'vue-countup-v3'import type { CountUpOptions } from 'vue-countup-v3'
Quickstart
<script setup lang="ts">
import { ref } from 'vue'
import CountUp from 'vue-countup-v3'
import type { ICountUp, CountUpOptions } from 'vue-countup-v3'
const endValueRef = ref(2022.22)
// coutup.js options
const options: CountUpOptions = {
separator: '❤️'
// ... other countUp.js options
}
let countUp: ICountUp | undefined
const onInit = (ctx: ICountUp) => {
console.log('init', ctx)
countUp = ctx
}
const onFinished = () => {
console.log('finished')
}
</script>
<template>
<count-up
:end-val="endValueRef"
:duration="2.5"
:decimal-places="2"
:options="options"
:loop="2"
:delay="2"
@init="onInit"
@finished="onFinished"></count-up>
</template>