Vue3使用transition實(shí)現(xiàn)組件切換的過渡效果
由于我想在項(xiàng)目中實(shí)現(xiàn)路由組件切換時(shí)的平滑過渡效果,以避免頁面加載時(shí)的突兀感,大致效果如下:
上面的代碼是使用的若依的代碼,代碼具體如下所示:
<section class="app-main"> <transition name="fade-transform" mode="out-in"> <keep-alive :include="cachedViews"> <router-view :key="key" /> </keep-alive> </transition> </section> <style> /* fade-transform */ .fade-transform-leave-active, .fade-transform-enter-active { transition: all .5s; } .fade-transform-enter { opacity: 0; transform: translateX(-30px); } .fade-transform-leave-to { opacity: 0; transform: translateX(30px); } /* breadcrumb transition */ .breadcrumb-enter-active, .breadcrumb-leave-active { transition: all .5s; } </style>
我的項(xiàng)目使用的是 VUE3 + TS,于是我仿照上面的寫法寫了下面的代碼:
<template> <section :class="[ sectionClass, 'flex-1 p-[var(--app-content-padding)] w-[calc(100%-var(--app-content-padding)-var(--app-content-padding))] bg-[var(--app-content-bg-color)] dark:bg-[var(--el-bg-color)]' ]" > <!-- 渲染路由視圖 --> <router-view v-slot="{ Component }"> <transition name="fade-transform" mode="out-in"> <keep-alive :include="getCaches"> <component :is="Component" :key="route.fullPath" /> </keep-alive> </transition> </router-view> </section> <Footer v-if="footer" :class="footerClass" /> </template> <style> /* slide-left */ .fade-transform-enter-active, .fade-transform-leave-active { transition: all 0.5s; } .fade-transform-enter { opacity: 0; transform: translateX(-10%); /* 進(jìn)入時(shí)從屏幕左側(cè)外部滑入 */ } .fade-transform-leave-to { opacity: 0; transform: translateX(10%); /* 離開時(shí)滑出到屏幕右側(cè)外部 */ } </style>
然而,在頁面渲染時(shí),我遇到了以下問題:
當(dāng)選擇“菜單管理”時(shí),控制臺(tái)打印出以下警告信息:
[Vue warn]: Component inside <Transition> renders non-element root node that cannot be animated.
此時(shí),當(dāng)我嘗試切換到其他路由頁面時(shí),頁面會(huì)顯示為空白。出現(xiàn)此問題的原因在于,Vue 3 中的 組件要求其子節(jié)點(diǎn)必須是一個(gè)元素節(jié)點(diǎn)。
然而,由于我在 <router-view>
組件中使用了 v-slot 來獲取路由組件的引用,并在 <component>
中渲染該引用,而我的 component 代碼中存在多個(gè)節(jié)點(diǎn),導(dǎo)致 Vue 報(bào)出錯(cuò)誤。
為了避免此問題,我將所有子組件都包裹在一個(gè)單一的根元素內(nèi),例如將原先的 Menu 組件改寫為如下代碼結(jié)構(gòu):
<template> <div class="app-container"> <!-- 菜單管理的組件內(nèi)容 --> </div> </template>
雖然解決了組件渲染的問題,但我又發(fā)現(xiàn),頁面切換時(shí)只有前一個(gè)組件消失時(shí)有過渡效果,而后一個(gè)組件顯示時(shí)卻是直接展現(xiàn),沒有過渡效果。如下所示:
進(jìn)入效果無效的解決辦法是我們需要手動(dòng)指定enter-from-class,如果不指定enter-form-class,則只有離開時(shí)候的動(dòng)畫有效。
這個(gè)在vue2中沒有出現(xiàn),vue3中是這樣的,而且只要進(jìn)入的這一步需要自己指定。
最后代碼如下:
<template> <section :class="[ sectionClass, 'flex-1 p-[var(--app-content-padding)] w-[calc(100%-var(--app-content-padding)-var(--app-content-padding))] bg-[var(--app-content-bg-color)] dark:bg-[var(--el-bg-color)]' ]" > <router-view v-slot="{ Component }"> <transition name="fade-transform" mode="out-in" enter-from-class="fade-transform-enter"> <keep-alive :include="getCaches"> <component :is="Component" :key="route.fullPath" /> </keep-alive> </transition> </router-view> </section> <Footer v-if="footer" :class="footerClass" /> </template> <style> /* slide-left */ .fade-transform-enter-active, .fade-transform-leave-active { transition: all 0.5s; } .fade-transform-enter { opacity: 0; transform: translateX(-100px); /* 進(jìn)入時(shí)從屏幕左側(cè)外部滑入 */ } .fade-transform-leave-to { opacity: 0; transform: translateX(10%); /* 離開時(shí)滑出到屏幕右側(cè)外部 */ } </style>
效果如下:
以上就是Vue3使用transition實(shí)現(xiàn)組件切換的過渡效果的詳細(xì)內(nèi)容,更多關(guān)于Vue3 transition組件切換過度的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue+vant移動(dòng)端顯示table表格加橫向滾動(dòng)條效果
vant移動(dòng)端顯示table效果,增加復(fù)選框,可以進(jìn)行多選和全選,加橫向滾動(dòng)條,可以看全部內(nèi)容,下面通過本文給大家分享vue+vant移動(dòng)端顯示table表格加橫向滾動(dòng)條效果,感興趣的朋友跟隨小編一起看看吧2024-06-06vue項(xiàng)目的屏幕自適應(yīng)多個(gè)方案總結(jié)
最近在用VUE寫大屏頁面,遇到屏幕自適應(yīng)問題,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目的屏幕自適應(yīng)多個(gè)方案的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06vue3第二次傳遞數(shù)據(jù)方法無法獲取到最新的值的解決方法
這篇文章主要介紹了vue3第二次傳遞數(shù)據(jù)方法無法獲取到最新的值,本文通過實(shí)例圖文相結(jié)合給大家詳細(xì)講解,感興趣的朋友一起看看吧2025-04-04Vue項(xiàng)目中npm?install卡住問題解決的詳細(xì)指南
這篇文章主要介紹了Vue項(xiàng)目中npm?install卡住問題解決的相關(guān)資料,文中包括更換npm鏡像源、清除npm緩存、刪除.npmrc文件和升級(jí)Node.js版本,需要的朋友可以參考下2024-12-12vue+element?upload上傳帶參數(shù)的實(shí)例
這篇文章主要介紹了vue+element?upload上傳帶參數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue下的elementui輪播圖自適應(yīng)高度問題
這篇文章主要介紹了vue下的elementui輪播圖自適應(yīng)高度問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09vue-autoui自匹配webapi的UI控件的實(shí)現(xiàn)
這篇文章主要介紹了vue-autoui自匹配webapi的UI控件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03vue3中的ref,toRef,toRefs三個(gè)的作用使用小結(jié)
Vue3中ref、reactive、toRef、toRefs都是與響應(yīng)式數(shù)據(jù)相關(guān)的,就此做一份筆記作為區(qū)別,本文重點(diǎn)給大家講解vue3中的ref,toRef,toRefs三個(gè)是干嘛的,有什么作用,感興趣的朋友跟隨小編一起看看吧2022-11-11Vue3+script setup+ts+Vite+Volar搭建項(xiàng)目
本文主要介紹了Vue3+script setup+ts+Vite+Volar搭建項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08