Skip to content

Commit f6abf56

Browse files
author
allen
committed
feat: 标签页优化
1 parent a1543fe commit f6abf56

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

src/@types/store.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
declare namespace App {
22
interface Tab {
33
title: string
4-
name: string
4+
fullPath: string
55
}
66
}
77

src/components/layout/Header.vue

+17-17
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ const breadcrumbArr = computed(() => {
2121
2222
// 切换tabs
2323
function tabClick(val: number | string = 0, delay = true) {
24-
let name: any
24+
let fullPath: any
2525
if (typeof val === 'number') {
26-
name = tabs.value[val].name
26+
fullPath = tabs.value[val].fullPath
2727
} else {
28-
name = val
28+
fullPath = val
2929
}
3030
setTimeout(
3131
() => {
3232
router.push({
33-
name,
33+
fullPath,
3434
})
3535
},
3636
delay ? 200 : 0,
@@ -42,19 +42,19 @@ const visible = ref(false)
4242
const top = ref(0)
4343
const left = ref(0)
4444
let clickName = ''
45-
function rightClick(e: { x: number; y: number }, name: string) {
46-
clickName = name
45+
function rightClick(e: { x: number; y: number }, fullPath: string) {
46+
clickName = fullPath
4747
left.value = e.x + 5
4848
top.value = e.y + 5
4949
visible.value = true
5050
}
5151
function closeMenu() {
5252
visible.value = false
5353
}
54-
function tabRemove(name: string = clickName) {
55-
const index = tabs.value.findIndex((i: App.Tab) => i.name === name)
56-
removeTab(name)
57-
if (route.name === name) {
54+
function tabRemove(fullPath: string = clickName) {
55+
const index = tabs.value.findIndex((i: App.Tab) => i.fullPath === fullPath)
56+
removeTab(fullPath)
57+
if (route.fullPath === fullPath) {
5858
const nextTabIndex = index > tabs.value.length - 1 ? tabs.value.length - 1 : index
5959
tabClick(nextTabIndex)
6060
}
@@ -65,7 +65,7 @@ function tabRemoveOther() {
6565
}
6666
function tabRemoveRight() {
6767
removeRightTab(clickName)
68-
if (tabs.value.every((i: App.Tab) => i.name !== route.name)) {
68+
if (tabs.value.every((i: App.Tab) => i.fullPath !== route.fullPath)) {
6969
tabClick(clickName)
7070
}
7171
}
@@ -127,23 +127,23 @@ function dragenter(e: { preventDefault: () => void }, index: number) {
127127
<transition-group name="tabs">
128128
<div
129129
v-for="(item, index) in tabs"
130-
:key="item.name"
130+
:key="item.fullPath"
131131
class="tab-item"
132-
:class="{ 'tab-active': item.name === route.name }"
132+
:class="{ 'tab-active': item.fullPath === route.fullPath }"
133133
draggable="true"
134-
@click="tabClick(item.name, false)"
135-
@contextmenu.prevent="rightClick($event, item.name)"
134+
@click="tabClick(item.fullPath, false)"
135+
@contextmenu.prevent="rightClick($event, item.fullPath)"
136136
@dragenter="dragenter($event, index)"
137137
@dragover="dragover($event)"
138138
@dragstart="dragstart(index)"
139139
>
140-
<div v-show="item.name === route.name" class="circle" />
140+
<div v-show="item.fullPath === route.fullPath" class="circle" />
141141
<div class="content">
142142
{{ item.title }}
143143
</div>
144144
<div
145145
class="carbon:close close-icon hover:carbon:close-filled"
146-
@click.stop="tabRemove(item.name)"
146+
@click.stop="tabRemove(item.fullPath)"
147147
/>
148148
</div>
149149
</transition-group>

src/layouts/default.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ const router = useRouter()
55
const routes = router.getRoutes()
66
const needCacheRouteNames = routes
77
.filter((item) => item.meta && !item.meta.notCache)
8-
.map((item) => item.name)
8+
.map((item) => item.fullPath)
99
1010
const cacheList = computed(() => {
1111
const arr = tabs.value
12-
.map((item) => item.name)
12+
.map((item) => item.fullPath.replace(/\?.*$/, ''))
1313
.filter((item) => needCacheRouteNames.includes(item))
1414
.map((item) => item.replace(/^\//, ''))
1515
return arr
@@ -29,7 +29,7 @@ const cacheList = computed(() => {
2929
<router-view v-slot="{ Component, route }">
3030
<transition name="fade-transform" mode="out-in">
3131
<keep-alive :include="cacheList">
32-
<component :is="Component" :key="route.name" />
32+
<component :is="Component" :key="route.fullPath" />
3333
</keep-alive>
3434
</transition>
3535
</router-view>

src/router/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ router.beforeEach((to, _from, next) => {
1818
if (!logged.value) return next('/login')
1919
const { addTab } = useStore('app')
2020
if (to.meta.title) {
21-
const tab = { title: to.meta.title, name: to.name } as App.Tab
21+
const tab = { title: to.meta.title, fullPath: to.fullPath } as App.Tab
2222
addTab(tab)
2323
}
2424
next()

src/store/app.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const defaultTab: App.Tab = { title: 'dashboard', name: '/' }
1+
const defaultTab: App.Tab = { title: 'dashboard', fullPath: '/' }
22

33
export default defineStore({
44
id: 'app',
@@ -17,20 +17,20 @@ export default defineStore({
1717
this.isCollapse = !this.isCollapse
1818
},
1919
addTab(tab: App.Tab = defaultTab) {
20-
if (this.tabs.some((t) => t.name === tab.name)) return
20+
if (this.tabs.some((t) => t.fullPath === tab.fullPath)) return
2121
this.tabs.push(tab)
2222
},
23-
removeTab(name: string) {
24-
this.tabs = this.tabs.filter((t) => t.name !== name)
23+
removeTab(fullPath: string) {
24+
this.tabs = this.tabs.filter((t) => t.fullPath !== fullPath)
2525
if (this.tabs.length === 0) {
2626
this.addTab()
2727
}
2828
},
29-
removeOtherTab(name: string) {
30-
this.tabs = this.tabs.filter((t) => t.name === name)
29+
removeOtherTab(fullPath: string) {
30+
this.tabs = this.tabs.filter((t) => t.fullPath === fullPath)
3131
},
32-
removeRightTab(name: string) {
33-
const index = this.tabs.findIndex((i) => i.name === name)
32+
removeRightTab(fullPath: string) {
33+
const index = this.tabs.findIndex((i) => i.fullPath === fullPath)
3434
this.tabs = this.tabs.slice(0, index + 1)
3535
},
3636
removeAllTab() {

0 commit comments

Comments
 (0)