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 = 
                            
                            

                        

相关文章

  • Vue3使用富文本框(wangeditor)的方法总结

    Vue3使用富文本框(wangeditor)的方法总结

    项目中用到了富文本,选来选去选择了wangeditor,下面这篇文章主要给大家介绍了关于Vue3使用富文本框(wangeditor)的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • Vue中路由传参的实用方式 分享

    Vue中路由传参的实用方式 分享

    这篇文章主要为大家详细介绍了VUE项目中路由之间的传值方式,文中的示例代码讲解详细,涉及到的方法也都是开发时常用的,希望对大家有多帮助
    2023-06-06
  • Vue2.x和Vue3.x的双向绑定原理详解

    Vue2.x和Vue3.x的双向绑定原理详解

    这篇文章主要给大家介绍了关于Vue2.x和Vue3.x的双向绑定原理的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Vue+Element树形表格实现拖拽排序示例

    Vue+Element树形表格实现拖拽排序示例

    本文主要介绍了Vue+Element树形表格实现拖拽排序示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • Vue 使用中的小技巧

    Vue 使用中的小技巧

    这篇文章主要介绍了Vue 使用中的小技巧,是小编日常开发的时候用到的小技巧,需要的朋友可以参考下
    2018-04-04
  • Vue3中的动态组件详解

    Vue3中的动态组件详解

    本文介绍了Vue3中的动态组件,通过`<component :is="动态组件名或组件对象"></component>`来实现根据条件动态渲染不同的组件,此外,还提到了使用`markRaw`和`shallowRef`来优化性能,避免不必要的响应式劫持
    2025-02-02
  • 源码分析Vue3响应式核心之effect

    源码分析Vue3响应式核心之effect

    这篇文章主要为大家详细介绍了Vue3响应式核心之effect的相关知识,文中的示例代码讲解详细,对我们学习Vue3有一定的帮助,需要的可以参考一下
    2023-04-04
  • Vue路由参数的传递与获取方式详细介绍

    Vue路由参数的传递与获取方式详细介绍

    顾名思义,vue路由传参是指嵌套路由时父路由向子路由传递参数,否则操作无效。传参方式可以划分为params传参和query传参,params传参又可以分为url中显示参数和不显示参数两种方式。具体区分和使用后续分析
    2022-09-09
  • vue中使用elementUI自定义校验及点击提交不生效问题解决办法

    vue中使用elementUI自定义校验及点击提交不生效问题解决办法

    我们在项目中经常会用到ElementUI的表单验证,下面这篇文章主要给大家介绍了关于vue中使用elementUI自定义校验及点击提交不生效问题解决的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-12-12
  • 详解vue beforeRouteEnter 异步获取数据给实例问题

    详解vue beforeRouteEnter 异步获取数据给实例问题

    这篇文章主要介绍了vue beforeRouteEnter 异步获取数据给实例问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08

最新评论