vue3 elementplus tabs切换实现

Tabs 标签页 | Element Plus

html 复制代码
<template>
  <!-- editableTabsValue 是当前tab 的 name -->
  <el-tabs
    v-model="editableTabsValue"
    type="border-card"
    editable
    @edit="handleTabsEdit"
  >
    <!-- 这个是标签面板 面板数据 遍历 editableTabs 渲染-->
    <el-tab-pane
      v-for="item in editableTabs"
      :key="item.name"
      :label="item.title"
      :name="item.name"
    >
      <!-- 页面内容 采用这种写法 包裹 keep-alive 标签是 组件保持keep-alive -->
      <router-view v-slot="{ Component }">
        <keep-alive>
          <component :is="Component" />
        </keep-alive>
      </router-view>
    </el-tab-pane>
  </el-tabs>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import type { TabPaneName } from 'element-plus'

import { useRoute, useRouter } from 'vue-router'
import { watch } from 'vue'

const route = useRoute()
const router = useRouter()

const initTab = {
  title: route.meta.title || 'New Tab',
  name: '1',
  url: route.fullPath,
}
// 使用对象张开运算符 复制 initTab 直接使用 inittab指向的是同一个对象
// editableTabs tabs 数据 是一个数组 里面是对象 每个对象是一个标签页
const editableTabs = ref([{ ...initTab }])
// tabindex 是最新标签 name 新标签为 tabIndex = tabIndex + 1
let tabIndex = editableTabs.value.length
// 当前的激活页 name
const editableTabsValue = ref(editableTabs.value[0].name)

// 监听 $route 对象的变化 如果路由发生变化 就更新 editableTabs 中的 url 和 title
watch(
  () => route.fullPath,
  (newPath) => {
    editableTabs.value.forEach((tab) => {
      if (tab.name === editableTabsValue.value) {
        if (tab.url === newPath) {
          return
        }
        tab.url = newPath
        tab.title = route.meta.title || 'New Tab'
      }
    })
  },
  { deep: true },
)
// 监听 editableTabsValue 的变化
//如果 editableTabsValue 变化了 就是切换了标签页 就更新路由到对应标签页
watch(
  () => editableTabsValue.value,
  (newValue) => {
    editableTabs.value.forEach((tab) => {
      if (tab.name === newValue) {
        if (tab.url === route.fullPath) {
          return
        }
        router.push(tab.url)
      }
    })
  },
)
// 处理标签页的编辑事件
// targetName 是当前标签页的 name
const handleTabsEdit = (
  targetName: TabPaneName | undefined,
  action: 'remove' | 'add',
) => {
  if (action === 'add') {
    const newTabName = `${++tabIndex}`
    const newtab = { ...initTab }
    newtab.name = newTabName
    editableTabs.value.push(newtab)
    editableTabsValue.value = newTabName
  } else if (action === 'remove') {
    const tabs = editableTabs.value
    let activeName = editableTabsValue.value
    // 如果删除的是当前激活的标签页,需要找到下一个标签页作为新的激活页
    if (activeName === targetName) {
      tabs.forEach((tab, index) => {
        if (tab.name === targetName) {
          const nextTab = tabs[index + 1] || tabs[index - 1]
          if (nextTab) {
            activeName = nextTab.name
          }
        }
      })
    }

    editableTabsValue.value = activeName
    editableTabs.value = tabs.filter((tab) => tab.name !== targetName)
  }
}
</script>
<style>
.el-tabs__new-tab {
  margin-right: 20px;
}
</style>
相关推荐
mayaairi12 分钟前
JS DOM属性操作完全指南:内容、样式与自定义属性
开发语言·前端·javascript
avi911117 分钟前
Threejs新版本后glb导出提示.x问题GLTFExporter,替代3dmax
开发语言·前端·javascript·threejs·glb·gltfexporter
用户298698530141 小时前
React 中 Word 文档转图片的实践方案
前端·javascript·react.js
Csvn1 小时前
异步并发控制:手写并发池、竞态防护与取消的完整方案
前端·javascript
csuzhucong1 小时前
二十一 - 四十阶魔方
javascript·css·html
专业抄代码选手1 小时前
06|双缓冲与 Commit:让 Fiber 支持第二次渲染
前端·javascript·react.js
天才熊猫君1 小时前
通用虚拟滚动表格行拖拽排序:从思路到完整实现
前端·javascript
xiaominlaopodaren1 小时前
three.js最小地图运行时(九):TileKey 线框与 cursor 坐标探针
javascript·gis·three.js
CarIise1 小时前
JavaScript进阶与轮播图实现 课堂笔记
开发语言·javascript·笔记
专业抄代码选手1 小时前
05|把递归渲染拆成 Fiber:让一棵大树可以暂停
前端·javascript·react.js