欢迎访问WDPHP素材源码!今天是2024年04月28日 星期日,傍晚好!下班了!
您好,游客 [ 马上登录 | 注册帐号 | 微信登录 | QQ登录]
当前位置:首页 > 教程 > 前端开发 > 

vue3怎么解决各场景loading过度
栏目分类:前端开发    发布日期:2023-07-22    浏览次数:401次     收藏

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

    vue3 常见过度

    1、 首次打开页面时 loading

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

    index.html
    文件

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

    当vue实例创建完成,通过

    .mount()
    方法挂载到
    id='app'
    的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>
    源码 模板 特效 素材 资源 教程 站长