Vue3使用transition實現(xiàn)組件切換的過渡效果
由于我想在項目中實現(xiàn)路由組件切換時的平滑過渡效果,以避免頁面加載時的突兀感,大致效果如下:
上面的代碼是使用的若依的代碼,代碼具體如下所示:
<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>
我的項目使用的是 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%); /* 進入時從屏幕左側(cè)外部滑入 */ } .fade-transform-leave-to { opacity: 0; transform: translateX(10%); /* 離開時滑出到屏幕右側(cè)外部 */ } </style>
然而,在頁面渲染時,我遇到了以下問題:
當選擇“菜單管理”時,控制臺打印出以下警告信息:
[Vue warn]: Component inside <Transition> renders non-element root node that cannot be animated.
此時,當我嘗試切換到其他路由頁面時,頁面會顯示為空白。出現(xiàn)此問題的原因在于,Vue 3 中的 組件要求其子節(jié)點必須是一個元素節(jié)點。
然而,由于我在 <router-view>
組件中使用了 v-slot 來獲取路由組件的引用,并在 <component>
中渲染該引用,而我的 component 代碼中存在多個節(jié)點,導致 Vue 報出錯誤。
為了避免此問題,我將所有子組件都包裹在一個單一的根元素內(nèi),例如將原先的 Menu 組件改寫為如下代碼結(jié)構(gòu):
<template> <div class="app-container"> <!-- 菜單管理的組件內(nèi)容 --> </div> </template>
雖然解決了組件渲染的問題,但我又發(fā)現(xiàn),頁面切換時只有前一個組件消失時有過渡效果,而后一個組件顯示時卻是直接展現(xiàn),沒有過渡效果。如下所示:
進入效果無效的解決辦法是我們需要手動指定enter-from-class,如果不指定enter-form-class,則只有離開時候的動畫有效。
這個在vue2中沒有出現(xiàn),vue3中是這樣的,而且只要進入的這一步需要自己指定。
最后代碼如下:
<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); /* 進入時從屏幕左側(cè)外部滑入 */ } .fade-transform-leave-to { opacity: 0; transform: translateX(10%); /* 離開時滑出到屏幕右側(cè)外部 */ } </style>
效果如下:
以上就是Vue3使用transition實現(xiàn)組件切換的過渡效果的詳細內(nèi)容,更多關于Vue3 transition組件切換過度的資料請關注腳本之家其它相關文章!
相關文章
vue3第二次傳遞數(shù)據(jù)方法無法獲取到最新的值的解決方法
這篇文章主要介紹了vue3第二次傳遞數(shù)據(jù)方法無法獲取到最新的值,本文通過實例圖文相結(jié)合給大家詳細講解,感興趣的朋友一起看看吧2025-04-04vue+element?upload上傳帶參數(shù)的實例
這篇文章主要介紹了vue+element?upload上傳帶參數(shù)的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04vue-autoui自匹配webapi的UI控件的實現(xiàn)
這篇文章主要介紹了vue-autoui自匹配webapi的UI控件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03vue3中的ref,toRef,toRefs三個的作用使用小結(jié)
Vue3中ref、reactive、toRef、toRefs都是與響應式數(shù)據(jù)相關的,就此做一份筆記作為區(qū)別,本文重點給大家講解vue3中的ref,toRef,toRefs三個是干嘛的,有什么作用,感興趣的朋友跟隨小編一起看看吧2022-11-11Vue3+script setup+ts+Vite+Volar搭建項目
本文主要介紹了Vue3+script setup+ts+Vite+Volar搭建項目,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08