vue3怎么解决各场景loading过度?
这篇文章主要介绍“vue3怎么解决各场景loading过度”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue3怎么解决各场景loading过度”文章能帮助大家解决问题。
vue3 常见过度
1、 首次打开页面时 loading
在页面首次打开的加载内容,是最容易的,通过根目录 index.html
文件
在 <div >
里添加内容,就是过度内容
<body> <div > <h2>加载中......</h2> </div> <script type="module" src="/src/main.js"></script> </body>
当vue实例创建完成,通过.mount()
方法挂载到 的div 里,会替换掉里的loading
内容;
2、 路由切换时、异步组件 loading
路由切换过度 需要先了解一个,
vue3
的内置组件<Suspense>
;<Suspense>
提供2
个插槽 ????;#default
: 一个要加载的内容 ;#fallback
: 一个加载种显示的内容;
<Suspense> <template #default> <router-view /> </template> <template #fallback> <h2>加载中......</h2> </template> </Suspense>
同理:( 异步组件的切换 )
<template> <Suspense> <template #default> <AsyncComp v-if = 'vitblie' /> </template> <template #fallback> <h2>加载中......</h2> </template> </Suspense> <button @click='open'> 切换 </button> </template> <script setup> import { defineAsyncComponent , ref } from 'vue'; const asyncComp = defineAsyncComponent(()=>important('./asyncComp.vue)); const vitblie = ref(false); function open(){ vitblie.value = !vitblie.value; } </script>
异步组件也是可以使用相同的方法
添加过度动画
添加过度动画需要先了解 vue3
内置组件 <Component>
和 <Transition>
????
<Component>
: 非常简单只有一个 is
显示该组件, 可以用来组件切换 如:
<template> <Component :is="visible ? TestComp : '' " /> </template>
<Transition>
: 里插入的内容 显示/隐藏 添加过度动画 ,通过 name
属性来拼接 class
如 :
<template> <transition name='anime'> <TestComp v-if='visblte' /> </transition> </template>
设置样式通过 name
属性 这里
anime-enter-active
: 过度态 ( 设置 隐藏 => 显示 过度的时间等参数)
anime-leave-active
: 过度态 ( 设置 显示 => 隐藏 过度的时间等参数)
anime-enter-from
=>anime-enter-to
隐藏 => 显示 开始和结束的样式
anime-leave-from
=>anime-leave-to
显示 => 隐藏 开始和结束的样式
组合起来 ????
<template> <router-view v-slot={ Component } > <transition name='anime'> <component :is="Component" /> <transition> </router-view> <template> <style scoped> .anime-enter-active, .anime-leave-active { transition: all 1s; } .anime-leave-from { transform: translateY(0); } .anime-leave-to { transform: translateY(-100%); } .anime-enter-from { transform: translateY(100%); } .anime-enter-to { transform: translate(0); } </style>
关于“vue3怎么解决各场景loading过度”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注***行业资讯频道,小编每天都会为大家更新不同的知识点。
- 上一篇 »Vue3中怎么引入Pinia存储库并使用?
- 下一篇 »拉去taro+vue3.0遇到的问题