Skip to content

Commit a1dfc36

Browse files
author
piexlMax(奇淼
committed
feat(路由): 添加配置控制标签页keep-alive功能
1 parent 6da1743 commit a1dfc36

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

web/src/core/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import packageInfo from '../../package.json'
55

66
const greenText = (text) => `\x1b[32m${text}\x1b[0m`
77

8-
const config = {
8+
export const config = {
99
appName: 'Gin-Vue-Admin',
1010
appLogo: 'logo.png',
1111
showViteLogo: true,
12+
KeepAliveTabs: true,
1213
logs: []
1314
}
1415

web/src/pinia/modules/router.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { defineStore } from 'pinia'
55
import { ref, watchEffect } from 'vue'
66
import pathInfo from '@/pathInfo.json'
77
import {useRoute} from "vue-router";
8+
import {config} from "@/core/config.js";
89

910
const notLayoutRouterArr = []
1011
const keepAliveRoutersArr = []
@@ -55,24 +56,24 @@ export const useRouterStore = defineStore('router', () => {
5556

5657
// 1. 首先添加原有的keepAlive配置
5758
keepArrTemp.push(...keepAliveRoutersArr)
58-
59-
history.forEach((item) => {
60-
// 2. 为所有history中的路由强制启用keep-alive
61-
// 通过routeMap获取路由信息,然后通过pathInfo获取组件名
62-
const routeInfo = routeMap[item.name]
63-
if (routeInfo && routeInfo.meta && routeInfo.meta.path) {
64-
const componentName = pathInfo[routeInfo.meta.path]
65-
if (componentName) {
66-
keepArrTemp.push(componentName)
59+
if (config.KeepAliveTabs) {
60+
history.forEach((item) => {
61+
// 2. 为所有history中的路由强制启用keep-alive
62+
// 通过routeMap获取路由信息,然后通过pathInfo获取组件名
63+
const routeInfo = routeMap[item.name]
64+
if (routeInfo && routeInfo.meta && routeInfo.meta.path) {
65+
const componentName = pathInfo[routeInfo.meta.path]
66+
if (componentName) {
67+
keepArrTemp.push(componentName)
68+
}
6769
}
68-
}
69-
70-
// 3. 如果子路由在tabs中打开,父路由也需要keepAlive
71-
if (nameMap[item.name]) {
72-
keepArrTemp.push(nameMap[item.name])
73-
}
74-
})
75-
70+
71+
// 3. 如果子路由在tabs中打开,父路由也需要keepAlive
72+
if (nameMap[item.name]) {
73+
keepArrTemp.push(nameMap[item.name])
74+
}
75+
})
76+
}
7677
keepAliveRouters.value = Array.from(new Set(keepArrTemp))
7778
}
7879

0 commit comments

Comments
 (0)