Lottie Animations for Vue 3
Vue3-lottie is a component library that enables developers to easily integrate Lottie animations into their Vue 3 and Nuxt 3 applications. It wraps `lottie-web` and provides a declarative API through a `<Vue3Lottie>` component, supporting a wide range of Lottie features like autoplay, looping, speed control, and segment playback. The current stable version is 3.3.1, with frequent patch and minor releases addressing bugs, improving types, and adding features. Key differentiators include its strong TypeScript support (enhanced in v3.0.0), seamless integration with the Vue ecosystem, and specific guidance for Nuxt 3. It abstracts away much of the complexity of directly using `lottie-web`, making it accessible for Vue developers. The library is actively maintained, ensuring compatibility with the latest Vue and Nuxt versions.
Common errors
-
TypeError: (0 , vue3_lottie__WEBPACK_IMPORTED_MODULE_0__.Vue3Lottie) is not a function
cause Attempting to import `Vue3Lottie` as a named export when it's a default export.fixChange the import statement from `import { Vue3Lottie } from 'vue3-lottie'` to `import Vue3Lottie from 'vue3-lottie'`. -
Property 'Vue3Lottie' was accessed during render but is not defined on instance.
cause The `Vue3Lottie` component was not correctly registered with the Vue application, common in Nuxt 3 or non-global registrations.fixIf in Nuxt 3, ensure you have a client-side plugin registering the component: `nuxtApp.vueApp.component('Vue3Lottie', Vue3Lottie);`. In a standard Vue CLI/Vite project, ensure it's imported and declared in the `components` option of your parent component or globally registered. -
Error: [Vue warn]: Failed to resolve component: Vue3Lottie
cause The `Vue3Lottie` component is not imported or registered in the component where it's being used.fixImport `Vue3Lottie` in your script section (`import Vue3Lottie from 'vue3-lottie';`) and register it in the `components` option of your Vue component: `{ components: { Vue3Lottie } }`.
Warnings
- breaking Version 3.0.0 introduced breaking changes, including enhanced TypeScript support and the removal of the `dist/style.css` import. Projects upgrading from v2.x must adjust their setup.
- gotcha The `Vue3Lottie` component is a default export, not a named export. Incorrectly importing it with curly braces will lead to a runtime error.
- gotcha When integrating into Nuxt 3, it's crucial to register `Vue3Lottie` as a global component via a Nuxt plugin. Directly using `nuxtApp.vueApp.use(Vue3Lottie)` will not work as expected.
- gotcha Animation data (the `animationData` prop) must be a parsed JSON object, not a string path to a JSON file. If loading from a URL, use the `animationLink` prop instead.
Install
-
npm install vue3-lottie -
yarn add vue3-lottie -
pnpm add vue3-lottie
Imports
- Vue3Lottie
import { Vue3Lottie } from 'vue3-lottie'import Vue3Lottie from 'vue3-lottie'
- Vue3Lottie (in Nuxt 3)
import Vue3Lottie from 'vue3-lottie'; export default defineNuxtPlugin((nuxtApp) => { nuxtApp.vueApp.use(Vue3Lottie); });import { defineNuxtPlugin } from '#app'; import Vue3Lottie from 'vue3-lottie'; export default defineNuxtPlugin((nuxtApp) => { nuxtApp.vueApp.component('Vue3Lottie', Vue3Lottie); }); - Props interface (TypeScript)
import type { Vue3LottieProps } from 'vue3-lottie'
Quickstart
<!-- src/App.vue or a component file -->
<template>
<div id="app">
<h1>My Lottie Animation</h1>
<Vue3Lottie
:animationData="AnimationData"
:height="200"
:width="200"
:loop="true"
:autoplay="true"
:speed="1"
:direction="direction"
@vnode-mounted="handleLottieMount"
/>
<button @click="toggleDirection">Toggle Direction</button>
<p>Current Direction: {{ direction }}</p>
</div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
import Vue3Lottie from 'vue3-lottie';
import AnimationData from './assets/lottie-animation.json'; // Assume you have a Lottie JSON file here
export default defineComponent({
name: 'App',
components: {
Vue3Lottie,
},
setup() {
const direction = ref(1); // 1 for forward, -1 for backward
const toggleDirection = () => {
direction.value = direction.value === 1 ? -1 : 1;
};
const handleLottieMount = (instance: any) => {
console.log('Lottie component mounted:', instance);
// You can access the lottie-web instance here if needed
};
return {
AnimationData,
direction,
toggleDirection,
handleLottieMount,
};
},
});
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>