Vue实现Tab标签路由效果并用Animate.css做转场动画效果的代码第2/3页
更新时间:2020年07月16日 10:31:44 作者:情绪羊
这篇文章主要介绍了Vue实现Tab标签路由效果,并用Animate.css做转场动画效果,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
// 设置pageList,这里清除其他,也就是保留自己
this.activePage = page.fullPath
this.$router.push(this.activePage).catch(e => e)
}
缓存控制
这部分逻辑比较简单,结合注释可以看懂
methods: {
clearCache (route) {
const componentName = last(route.matched).components.default.name // last方法来自lodash,获取数组最后一个元素
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中删除当前组件名,恢复其缓存机制
}
}
}
这样,主要逻辑就做完了,下面简单说说转场动画的实现
转场动画实现
转场动画主要是用到 Animate.css 配合Vue的 transition 组件实现,组件完整代码如下,极其简单:
<template>
<transition :enter-active-class="`animate__animated animate__${name}`">
<slot />
</transition>
</template>
<script>
export default {
name: 'PageToggleTransition',
props: {
name: String
}
}
</script>
具体参考官方文档 关于transition组件的说明
最后 借鉴自 vue-antd-admin github.com/1446445040/…
完整代码
<template>
<PageLayout>
<ContextMenu
:list="menuItems"
:visible.sync="menuVisible"
@select="onMenuSelect"
/>
<!-- 标签部分 -->
<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: '关闭左侧' },
{ key: '2', icon: 'arrow-right', text: '关闭右侧' },
{ key: '3', icon: 'close', text: '关闭其它' }
]
}
},
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)
// 如果删除的是当前页才需要跳转
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) // 重置,否则会影响到某些组件的缓存
})
}
},
// 自定义右键菜单的关闭功能
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 =
相关文章
vue中使用elementUI自定义校验及点击提交不生效问题解决办法
我们在项目中经常会用到ElementUI的表单验证,下面这篇文章主要给大家介绍了关于vue中使用elementUI自定义校验及点击提交不生效问题解决的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下2023-12-12
详解vue beforeRouteEnter 异步获取数据给实例问题
这篇文章主要介绍了vue beforeRouteEnter 异步获取数据给实例问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-08-08


最新评论