Skip to content

Commit 1dba039

Browse files
author
allen
committed
fix: keep alive
1 parent 76479a6 commit 1dba039

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

src/components/layout/Header.vue

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import type { RouteLocationRaw } from 'vue-router'
2+
import type { RouteLocationRaw, RouteRecordNameGeneric } from 'vue-router'
33
import { onBeforeRouteUpdate } from 'vue-router'
44
55
const { logout } = useStore('user')
@@ -25,22 +25,24 @@ const breadcrumbArr = computed(() => {
2525
2626
// 更新头部tabs
2727
onBeforeRouteUpdate((to, _from, next) => {
28-
const tab = { title: to.meta.title, name: to.path }
28+
const tab = { title: to.meta.title, name: to.name }
2929
addTab(tab)
3030
next()
3131
})
3232
3333
// 切换tabs
3434
function tabClick(val: number | string = 0, delay = true) {
35-
let path: RouteLocationRaw
35+
let name: RouteRecordNameGeneric
3636
if (typeof val === 'number') {
37-
path = tabs.value[val].name
37+
name = tabs.value[val].name
3838
} else {
39-
path = val
39+
name = val
4040
}
4141
setTimeout(
4242
() => {
43-
router.push(path)
43+
router.push({
44+
name,
45+
})
4446
},
4547
delay ? 200 : 0,
4648
)
@@ -63,7 +65,7 @@ function closeMenu() {
6365
function tabRemove(name: string = clickName) {
6466
const index = tabs.value.findIndex((i: App.Tab) => i.name === name)
6567
removeTab(name)
66-
if (route.path === name) {
68+
if (route.name === name) {
6769
const nextTabIndex = index > tabs.value.length - 1 ? tabs.value.length - 1 : index
6870
tabClick(nextTabIndex)
6971
}
@@ -74,7 +76,7 @@ function tabRemoveOther() {
7476
}
7577
function tabRemoveRight() {
7678
removeRightTab(clickName)
77-
if (tabs.value.every((i: App.Tab) => i.name !== route.path)) {
79+
if (tabs.value.every((i: App.Tab) => i.name !== route.name)) {
7880
tabClick(clickName)
7981
}
8082
}
@@ -138,15 +140,15 @@ function dragenter(e: { preventDefault: () => void }, index: number) {
138140
v-for="(item, index) in tabs"
139141
:key="item.name"
140142
class="tab-item"
141-
:class="{ 'tab-active': item.name === route.path }"
143+
:class="{ 'tab-active': item.name === route.name }"
142144
draggable="true"
143145
@click="tabClick(item.name, false)"
144146
@contextmenu.prevent="rightClick($event, item.name)"
145147
@dragenter="dragenter($event, index)"
146148
@dragover="dragover($event)"
147149
@dragstart="dragstart(index)"
148150
>
149-
<div v-show="item.name === route.path" class="circle" />
151+
<div v-show="item.name === route.name" class="circle" />
150152
<div class="content">
151153
{{ item.title }}
152154
</div>

src/pages/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const { tabs } = useStore('app')
1414
<el-main>
1515
<router-view v-slot="{ Component, route }">
1616
<transition name="fade-transform" mode="out-in">
17-
<keep-alive :include="tabs.map((i: any) => i.name.slice(1))">
17+
<keep-alive :include="tabs.map((i: any) => i.name)">
1818
<component :is="Component" :key="route.name" />
1919
</keep-alive>
2020
</transition>

src/store/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const defaultTab: App.Tab = { title: 'dashboard', name: '/dashboard' }
1+
const defaultTab: App.Tab = { title: 'dashboard', name: 'dashboard' }
22

33
export default defineStore({
44
id: 'app',

0 commit comments

Comments
 (0)