vue3使用Lottie

第一步:安装lottie

// "lottie-web": "^5.9.6",
npm i lottie-web

第二步:封装lottie.vue

<template>
    <div ref="animation" :style="{ width, height }"></div>
</template>

<script>

    import {defineComponent, ref, onMounted} from 'vue'

    import lottie from 'lottie-web'

    export default defineComponent({
        name: 'Lottie',
        props: {
            width: {
                type: String,
                default: '100px',
            },
            height: {
                type: String,
                default: '100px',
            },
            src: {
                type: String,
                default: "https://assets10.lottiefiles.com/packages/lf20_dyiqnus5.json",
            },
            jsonData: {
                type: Object,
                default: () => null,
            },
            autoplay: {
                type: Boolean,
                default: true,
            },
            loop: {
                type: Boolean,
                default: true,
            },
        },
        setup(props) {
            let animation = ref(null)

            onMounted(() => {
                if (animation.value) {
                    lottie.loadAnimation({
                        container: animation.value,
                        renderer: "svg",
                        loop: props.loop,
                        autoplay: props.autoplay,
                        path: props.src,
                        // animationData只能加载本地json,优先级高于path
                        animationData: props.jsonData,
                    })
                }
            });

            return {
                animation
            }
        }
    })


</script>

<style scoped>

</style>

第三步:将封装好的lottie.vue导出

import Lottie from './src/lottie'
export { Lottie }

第四步:404.vue引入lottie.vue & 路由配置

<template>
    <div class="errPage">
        <div style="margin-top: 100px">
            <Lottie
                    width="400px"
                    height="400px"
                    src="https://assets9.lottiefiles.com/packages/lf20_suhe7qtm.json"
            />
            <div class="info">抱歉,您访问的页面不存在。</div>
            <div class="goback">
                <el-button @Click="goBack">返回上一页</el-button>
                <el-button type="primary" @Click="goHome"> 返回首页 </el-button>
            </div>
        </div>
    </div>
</template>

<script setup>
    import {Lottie} from '@/components/lottie'
    import { useRouter } from "vue-router";

    const router = useRouter();

    function goHome() {
        router.push("/");
    }

    function goBack() {
        router.go(-1);
    }
</script>

<style lang="scss" scoped>

    .errPage {
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-wrap: wrap;

        .info {
            text-align: center;
            font-size: 16px;
            padding: 20px 0;
            color: #999;
        }

        .goback {
            width: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
        }
    }

</style>

const routes = [
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/Login')
    },
    {
        path: '/',
        name: 'layout',
        component: () => import('@/layout'),
        children: [
            {
                path: 'menu',
                name: 'menu',
                component: () => import('@/views/menu'),
            }
        ]
    },
    {
        path: '/:pathMatch(.*)*',
        name: '404',
        component:() => import('@/views/404')
    }
]

第五步:将json地址传给组件的src属性

在这里插入图片描述

效果

在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue3-lottie是一个用于在Vue3中使用Lottie动画的插件。安装它的步骤如下: 1. 首先,确保你已经安装了npm、cnpm、pnpm或yarn,这些都是常用的包管理工具,你可以根据自己的喜好选择其中一个来安装依赖。 2. 在你的项目中,打开终端并执行以下命令来安装vue3-lottie插件:npm install vue3-lottie@latest 或者 cnpm install vue3-lottie@latest 或者 pnpm install vue3-lottie@latest 或者 yarn add vue3-lottie@latest。 3. 安装完成后,你可以在你的项目中使用vue3-lottie插件了。 关于Lottie动画文件的获取,你可以访问https://lottiefiles.com/featured 网站来获取一些精选的Lottie动画文件。在这个网站上,你可以找到各种各样的动画效果,并下载对应的json文件。 在使用vue3-lottie插件时,你只需要将下载的Lottie动画的json文件导入到你的Vue组件中,并将其渲染到界面上即可。你可以参考Lottie官方地址来了解更多关于该插件的详细信息。 所以,总结起来,安装vue3-lottie的步骤是:1、安装npm/cnpm/pnpm/yarn;2、下载Lottie动画的json文件;3、将json文件导入到Vue组件中并渲染。 希望能对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [vue3+lottie使用神器:vue3-lottie (electron也适用)](https://blog.csdn.net/qq_41619796/article/details/131866960)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值