Vue Lottie Player
Vue Lottie Player is a type-safe Vue 3 component and plugin for integrating Lottie animations into web applications, built as a wrapper around the `lottie-web` library. The current stable version is 2.0.2. This library prioritizes security and performance by offering a default 'light-player' build that is CSP-friendly and avoids `unsafe-eval`, alongside an opt-in '/full' entry for animations requiring After Effects expressions. It provides a robust API for both URL- and JSON-based animation sources, imperative ref controls for direct playback manipulation, and straightforward integration with Nuxt 3. Its key differentiators include built-in TypeScript types, a focus on CSP compliance, and a clear separation between a lean default build and a feature-rich full build.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'play')
cause Attempting to call imperative methods (play, pause, stop) on the Lottie player instance before it has fully initialized or if the ref is not correctly bound.fixEnsure the Lottie player ref is initialized (`ref<VueLottiePlayerInstance | null>(null)`) and that methods are called only after the component is mounted or the `@ready` event has fired. Check that `player.value` is not null before calling methods. -
[Vue warn]: Failed to resolve component: VueLottiePlayer
cause The `VueLottiePlayer` component was not imported or globally registered correctly.fixIf using as a component, ensure `import { VueLottiePlayer } from 'vue-lottie-player';` is present in your script. If intended for global use, ensure `createApp(App).use(VueLottiePlayerPlugin).mount('#app');` is configured. -
Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source...
cause This CSP error occurs when trying to play an animation that uses After Effects expressions with the default 'light-player' build, which disallows `unsafe-eval`.fixIf your animation requires expressions, switch to the full build: `import { VueLottiePlayer } from 'vue-lottie-player/full';`. Otherwise, simplify your Lottie animation to avoid expressions.
Warnings
- breaking Vue 3.4.0 or newer is required. Older Vue 3 versions are not supported due to internal API changes.
- gotcha The default build avoids `unsafe-eval` for CSP compliance, which means animations relying on After Effects expressions might not render correctly or at all. This is by design for security.
- gotcha When using `vue-lottie-player` in Nuxt 3, it must be registered as a client-side plugin and the component rendered within `<ClientOnly>` tags. `lottie-web` relies on browser APIs and cannot be run during SSR.
- gotcha The component requires a stylesheet to be imported once in your application for proper rendering. Failing to do so may result in invisible or incorrectly sized players.
Install
-
npm install vue-lottie-player -
yarn add vue-lottie-player -
pnpm add vue-lottie-player
Imports
- VueLottiePlayer
import VueLottiePlayer from 'vue-lottie-player';
import { VueLottiePlayer } from 'vue-lottie-player'; - VueLottiePlayerPlugin
import { VueLottiePlayerPlugin } from 'vue-lottie-player'; - LottieUrlSource
import type { LottieUrlSource } from 'vue-lottie-player'; - style.css
import 'vue-lottie-player/style.css';
Quickstart
import { ref } from 'vue';
import type { AnimationItem } from 'lottie-web';
import 'vue-lottie-player/style.css';
import type { LottieUrlSource, LottieDataSource, VueLottiePlayerInstance } from 'vue-lottie-player';
import { VueLottiePlayer } from 'vue-lottie-player';
// Assuming you have an animation JSON file at /animations/sample-animation.json
// Or if using local data:
// import demoAnimationData from './assets/demo-animation.json';
const sourceUrl: LottieUrlSource = {
kind: 'url',
value: '/animations/sample-animation.json'
};
// Example for local JSON data:
// const sourceData: LottieDataSource = {
// kind: 'data',
// value: demoAnimationData
// };
const playerRef = ref<VueLottiePlayerInstance | null>(null);
function onReady(animation: AnimationItem) {
console.log('Lottie animation is ready:', animation);
animation.setSpeed(1.5);
}
function playAnimation() {
playerRef.value?.play();
}
function pauseAnimation() {
playerRef.value?.pause();
}
function stopAnimation() {
playerRef.value?.stop();
}