|
|
@@ -0,0 +1,113 @@
|
|
|
+<script lang="ts" setup>
|
|
|
+import type { TabPanelName } from 'element-plus'
|
|
|
+import type { RouteLocationNormalizedLoaded } from 'vue-router'
|
|
|
+import config from '@/config/defaultSetting'
|
|
|
+import { useRouterStore } from '@/stores/router'
|
|
|
+
|
|
|
+const router = useRouter()
|
|
|
+const route = useRoute()
|
|
|
+const routerStore = useRouterStore()
|
|
|
+
|
|
|
+const tabs = ref<any[]>([])
|
|
|
+const activeValue = ref(route.fullPath)
|
|
|
+const contextMenuVisible = ref(false)
|
|
|
+
|
|
|
+const getHomeRoute = (route: []) => {
|
|
|
+ return route.find((item: any) => {
|
|
|
+ if (item.children) {
|
|
|
+ getHomeRoute(item.children)
|
|
|
+ } else {
|
|
|
+ return item.name === config.homeRoute
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+watch(
|
|
|
+ () => route,
|
|
|
+ to => {
|
|
|
+ setTab(to)
|
|
|
+ },
|
|
|
+ {
|
|
|
+ deep: true
|
|
|
+ }
|
|
|
+)
|
|
|
+watch(
|
|
|
+ tabs,
|
|
|
+ () => {
|
|
|
+ sessionStorage.setItem('globalTabs', JSON.stringify(tabs.value))
|
|
|
+ },
|
|
|
+ {
|
|
|
+ deep: true
|
|
|
+ }
|
|
|
+)
|
|
|
+const setTab = (tab: RouteLocationNormalizedLoaded) => {
|
|
|
+ activeValue.value = tab.fullPath
|
|
|
+
|
|
|
+ if (tab.name === 'login' || tabs.value.find(item => item.fullPath === tab.fullPath)) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ tabs.value.push(routeToTab(tab))
|
|
|
+}
|
|
|
+const routeToTab = (route: any) => {
|
|
|
+ return {
|
|
|
+ name: route.name,
|
|
|
+ fullPath: route.fullPath,
|
|
|
+ params: route.params,
|
|
|
+ query: route.query,
|
|
|
+ title: route.meta.title
|
|
|
+ }
|
|
|
+}
|
|
|
+tabs.value.push(routeToTab(getHomeRoute(routerStore.menuRouter)))
|
|
|
+console.log(routeToTab(getHomeRoute(routerStore.menuRouter)))
|
|
|
+const initTabs = () => {
|
|
|
+ tabs.value = JSON.parse(sessionStorage.getItem('globalTabs') || '[]')
|
|
|
+ activeValue.value = window.sessionStorage.getItem('activeValue') || route.fullPath
|
|
|
+}
|
|
|
+initTabs()
|
|
|
+
|
|
|
+const changeTab = (name: TabPanelName) => {
|
|
|
+ router.push(tabs.value.find(item => item.fullPath === name))
|
|
|
+ sessionStorage.setItem('activeValue', name as string)
|
|
|
+}
|
|
|
+const removeTab = () => {}
|
|
|
+const openContextMenu = (event: any) => {}
|
|
|
+</script>
|
|
|
+
|
|
|
+<template>
|
|
|
+ <div class="bg-white" style="border-top: 1px solid var(--el-border-color-light)">
|
|
|
+ <el-tabs
|
|
|
+ v-model="activeValue"
|
|
|
+ :closable="!(tabs.length === 1)"
|
|
|
+ type="card"
|
|
|
+ @contextmenu.prevent="openContextMenu($event)"
|
|
|
+ @tab-change="changeTab"
|
|
|
+ @tab-remove="removeTab"
|
|
|
+ >
|
|
|
+ <el-tab-pane v-for="item in tabs" :key="item.name" :label="item.title" :name="item.fullPath" :tab="item">
|
|
|
+ </el-tab-pane>
|
|
|
+ </el-tabs>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <!--自定义右键菜单html代码-->
|
|
|
+ <!-- <ul
|
|
|
+ v-show="contextMenuVisible"
|
|
|
+ :style="{ left: left + 'px', top: top + 'px' }"
|
|
|
+ class="contextmenu"
|
|
|
+ >
|
|
|
+ <li @click="closeAll">关闭所有</li>
|
|
|
+ <li @click="closeLeft">关闭左侧</li>
|
|
|
+ <li @click="closeRight">关闭右侧</li>
|
|
|
+ <li @click="closeOther">关闭其他</li>
|
|
|
+ </ul> -->
|
|
|
+</template>
|
|
|
+
|
|
|
+<style lang="scss" scoped>
|
|
|
+:deep(.el-tabs__header) {
|
|
|
+ margin: 0;
|
|
|
+}
|
|
|
+:deep(.el-tabs__nav) {
|
|
|
+ border-top: none !important;
|
|
|
+ border-radius: 0 !important;
|
|
|
+}
|
|
|
+</style>
|