# Vue3-lottie 动画的使用
URL: /%E6%96%87%E6%A1%A3/04.%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/01.%20%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91/13.%20H5%E9%80%9A%E7%94%A8%E6%8A%80%E6%9C%AF/03.%20Vue3-lottie%E5%8A%A8%E7%94%BB


# Vue3-lottie 动画的使用

Vue

## 安装

```js
yarn add vue3-lottie@latest
// or
npm install vue3-lottie@latest --save
```

## 全局注册

main.js

```js
import { createApp } from 'vue'
import Vue3Lottie from 'vue3-lottie'
import 'vue3-lottie/dist/style.css'

const app = createApp(App)
app.use(Vue3Lottie).mount('#app')

// 1.重命名组件
// app.use(Vue3Lottie, { name: 'LottieAnimation' }) 
// 2.使用
// <LottieAnimation />
```

components.d.ts

```js
// 要为Volar类型检查定义全局组件，需要添加：
declare module '@vue/runtime-core' {
  export interface GlobalComponents {
    LottieAnimation: typeof import('vue3-lottie')['Vue3Lottie']
  }
}

declare module '*.json' {
  const value: any;
  export default value;
}

export {}
```

## 使用组件

```js
<template>
  <Vue3Lottie :animationData="DogJSON" :height="200" :width="200" />
</template>
 
<script setup>
import DogJSON from './lotties/dog.json'
</script>
```

## 相关配置

| Prop            | 类型              | 默认值      | 描述                                   |
| --------------- | ----------------- | ----------- | -------------------------------------- |
| animationData   | Object            | {}          | 作为 JSON 对象提供的 lottie 动画数据       |
| animationLink   | String            | ''          | Lottie 动画数据的 URL 链接                |
| width           | Number or String  | "100%"      | lottie 动画容器的宽度 (数字对应像素值)   |
| height          | Number or String  | "100%"      | lottie 动画容器的高度 (数字对应像素值)   |
| speed           | Number            | "1"         | lottie 动画的速度                       |
| direction       | String            | "forward"   | 动画播放方向                           |
| loop            | Number or Boolean | true        | lottie 动画应该运行的实例数 (true 为无限) |
| autoPlay        | Boolean           | true        | 在组件加载时启动动画                   |
| delay           | Number            | 0           | 延迟动画播放状态几毫秒                 |
| pauseOnHover    | Boolean           | false       | 是否在悬停时暂停动画                   |
| playOnHover     | Boolean           | false       | 当鼠标悬停时是否播放动画               |
| backgroundColor | String            | transparent | 容器的背景颜色                         |
| renderer        | String            | "svg"       | 设置渲染器                             |
