vue3怎么解决各场景loading过度

其他教程   发布日期:2024年04月20日   浏览次数:463

这篇文章主要介绍“vue3怎么解决各场景loading过度”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue3怎么解决各场景loading过度”文章能帮助大家解决问题。

    vue3 常见过度

    1、 首次打开页面时 loading

    在页面首次打开的加载内容,是最容易的,通过根目录

    1. index.html
    文件

    1. <div id='app'>
    里添加内容,就是过度内容
    1. <body>
    2. <div id="app">
    3. <h2>加载中......</h2>
    4. </div>
    5. <script type="module" src="/src/main.js"></script>
    6. </body>

    当vue实例创建完成,通过

    1. .mount()
    方法挂载到
    1. id='app'
    的div 里,会替换掉里的
    1. loading
    内容;

    2、 路由切换时、异步组件 loading

    • 路由切换过度 需要先了解一个,

      1. vue3
      的内置组件
      1. <Suspense>
      1. <Suspense>
      提供
      1. 2
      个插槽 ????;
      1. #default
      : 一个要加载的内容 ;
      1. #fallback
      : 一个加载种显示的内容;
    1. <Suspense>
    2. <template #default>
    3. <router-view />
    4. </template>
    5. <template #fallback>
    6. <h2>加载中......</h2>
    7. </template>
    8. </Suspense>

    同理:( 异步组件的切换 )

    1. <template>
    2. <Suspense>
    3. <template #default>
    4. <AsyncComp v-if = 'vitblie' />
    5. </template>
    6. <template #fallback>
    7. <h2>加载中......</h2>
    8. </template>
    9. </Suspense>
    10. <button @click='open'> 切换 </button>
    11. </template>
    12. <script setup>
    13. import { defineAsyncComponent , ref } from 'vue';
    14. const asyncComp = defineAsyncComponent(()=>important('./asyncComp.vue));
    15. const vitblie = ref(false);
    16. function open(){
    17. vitblie.value = !vitblie.value;
    18. }
    19. </script>

    异步组件也是可以使用相同的方法

    添加过度动画

    添加过度动画需要先了解

    1. vue3
    内置组件
    1. <Component>
    1. <Transition>
    ????

    1. <Component>
    : 非常简单只有一个
    1. is
    显示该组件, 可以用来组件切换 如:
    1. <template>
    2. <Component :is="visible ? TestComp : '' " />
    3. </template>

    1. <Transition>
    : 里插入的内容 显示/隐藏 添加过度动画 ,通过
    1. name
    属性来拼接
    1. class
    如 :
    1. <template>
    2. <transition name='anime'>
    3. <TestComp v-if='visblte' />
    4. </transition>
    5. </template>

    设置样式通过

    1. name
    属性 这里

    1. anime-enter-active
    : 过度态 ( 设置 隐藏 => 显示 过度的时间等参数)
    1. anime-leave-active
    : 过度态 ( 设置 显示 => 隐藏 过度的时间等参数)

    1. anime-enter-from
    =>
    1. anime-enter-to
    隐藏 => 显示 开始和结束的样式
    1. anime-leave-from
    =>
    1. anime-leave-to
    显示 => 隐藏 开始和结束的样式

    组合起来 ????

    1. <template>
    2. <router-view v-slot={ Component } >
    3. <transition name='anime'>
    4. <component :is="Component" />
    5. <transition>
    6. </router-view>
    7. <template>
    8. <style scoped>
    9. .anime-enter-active,
    10. .anime-leave-active {
    11. transition: all 1s;
    12. }
    13. .anime-leave-from { transform: translateY(0); }
    14. .anime-leave-to { transform: translateY(-100%); }
    15. .anime-enter-from { transform: translateY(100%); }
    16. .anime-enter-to { transform: translate(0); }
    17. </style>

    以上就是vue3怎么解决各场景loading过度的详细内容,更多关于vue3怎么解决各场景loading过度的资料请关注九品源码其它相关文章!