Vue實(shí)現(xiàn)Tab標(biāo)簽路由效果并用Animate.css做轉(zhuǎn)場(chǎng)動(dòng)畫效果的代碼第2/3頁
緩存控制
這部分邏輯比較簡(jiǎn)單,結(jié)合注釋可以看懂
methods: {
clearCache (route) {
const componentName = last(route.matched).components.default.name // last方法來自lodash,獲取數(shù)組最后一個(gè)元素
this.dustbin.push(componentName) // 清除
},
putCache (route) {
const componentName = last(route.matched).components.default.name
if (this.dustbin.includes(componentName)) {
this.dustbin = this.dustbin.filter(item => item !== componentName) // 從dustbin中刪除當(dāng)前組件名,恢復(fù)其緩存機(jī)制
}
}
}
這樣,主要邏輯就做完了,下面簡(jiǎn)單說說轉(zhuǎn)場(chǎng)動(dòng)畫的實(shí)現(xiàn)
轉(zhuǎn)場(chǎng)動(dòng)畫實(shí)現(xiàn)
轉(zhuǎn)場(chǎng)動(dòng)畫主要是用到 Animate.css 配合Vue的 transition 組件實(shí)現(xiàn),組件完整代碼如下,極其簡(jiǎn)單:
<template>
<transition :enter-active-class="`animate__animated animate__${name}`">
<slot />
</transition>
</template>
<script>
export default {
name: 'PageToggleTransition',
props: {
name: String
}
}
</script>
具體參考官方文檔 關(guān)于transition組件的說明
最后 借鑒自 vue-antd-admin github.com/1446445040/…
完整代碼
<template>
<PageLayout>
<ContextMenu
:list="menuItems"
:visible.sync="menuVisible"
@select="onMenuSelect"
/>
<!-- 標(biāo)簽部分 -->
<a-tabs
type="editable-card"
:hide-add="true"
:active-key="activePage"
@change="changePage"
@edit="editPage"
@contextmenu="onContextmenu"
>
<a-tab-pane v-for="page in pageList" :key="page.fullPath">
<template #tab>
<span :data-key="page.fullPath">
{{ page.name }}
</span>
</template>
</a-tab-pane>
</a-tabs>
<!-- 路由出口 -->
<PageToggleTransition name="fadeIn">
<keep-alive :exclude="dustbin">
<router-view />
</keep-alive>
</PageToggleTransition>
</PageLayout>
</template>
<script>
import { message } from 'ant-design-vue'
import { last } from 'lodash'
import PageLayout from './PageLayout'
import ContextMenu from '../components/common/ContextMenu'
import PageToggleTransition from '../components/transition/PageToggleTransition'
export default {
name: 'TabLayout',
components: { PageToggleTransition, ContextMenu, PageLayout },
data () {
return {
pageList: [],
dustbin: [],
activePage: '',
menuVisible: false,
menuItems: [
{ key: '1', icon: 'arrow-left', text: '關(guān)閉左側(cè)' },
{ key: '2', icon: 'arrow-right', text: '關(guān)閉右側(cè)' },
{ key: '3', icon: 'close', text: '關(guān)閉其它' }
]
}
},
watch: {
$route: {
handler (route) {
this.activePage = route.fullPath
this.putCache(route)
const index = this.pageList.findIndex(item => item.fullPath === route.fullPath)
if (index === -1) {
this.pageList.push(route)
}
},
immediate: true
}
},
methods: {
changePage (key) {
this.activePage = key
this.$router.push(key)
},
editPage (key, action) {
if (action === 'remove') {
this.remove(key)
}
},
remove (key) {
if (this.pageList.length <= 1) {
return message.info('最后一頁了哦~')
}
let curIndex = this.pageList.findIndex(item => item.fullPath === key)
const { matched } = this.pageList[curIndex]
const componentName = last(matched).components.default.name
this.dustbin.push(componentName)
this.pageList.splice(curIndex, 1)
// 如果刪除的是當(dāng)前頁才需要跳轉(zhuǎn)
if (key === this.activePage) {
// 判斷向左跳還是向右跳
curIndex = curIndex >= this.pageList.length ? this.pageList.length - 1 : curIndex
const page = this.pageList[curIndex]
this.$router.push(page.fullPath).finally(() => {
this.dustbin.splice(0) // 重置,否則會(huì)影響到某些組件的緩存
})
}
},
// 自定義右鍵菜單的關(guān)閉功能
onContextmenu (e) {
const key = getTabKey(e.target)
if (!key) return
e.preventDefault()
this.menuVisible = true
},
onMenuSelect (key, target) {
const tabKey = getTabKey(target)
switch (key) {
case '1': this.closeLeft(tabKey); break
case '2': this.closeRight(tabKey); break
case '3': this.closeOthers(tabKey); break
default: break
}
},
closeOthers (tabKey) {
const index = this.pageList.findIndex(item => item.fullPath === tabKey)
for (const route of this.pageList) {
if (route.fullPath !== tabKey) {
this.clearCache(route)
}
}
const page = this.pageList[index]
this.pageList =
相關(guān)文章
Vue3使用富文本框(wangeditor)的方法總結(jié)
項(xiàng)目中用到了富文本,選來選去選擇了wangeditor,下面這篇文章主要給大家介紹了關(guān)于Vue3使用富文本框(wangeditor)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Vue+Element樹形表格實(shí)現(xiàn)拖拽排序示例
本文主要介紹了Vue+Element樹形表格實(shí)現(xiàn)拖拽排序示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
vue中使用elementUI自定義校驗(yàn)及點(diǎn)擊提交不生效問題解決辦法
我們?cè)陧?xiàng)目中經(jīng)常會(huì)用到ElementUI的表單驗(yàn)證,下面這篇文章主要給大家介紹了關(guān)于vue中使用elementUI自定義校驗(yàn)及點(diǎn)擊提交不生效問題解決的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
詳解vue beforeRouteEnter 異步獲取數(shù)據(jù)給實(shí)例問題
這篇文章主要介紹了vue beforeRouteEnter 異步獲取數(shù)據(jù)給實(shí)例問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

