Commit b884654761f93455014fd1dcb0e40c030d8fb360

Authored by vben
1 parent 4cca0071

fix: fix keepAlive not work

src/layouts/page/index.tsx
@@ -45,7 +45,7 @@ export default defineComponent({ @@ -45,7 +45,7 @@ export default defineComponent({
45 const renderComp = () => <Component key={route.fullPath} />; 45 const renderComp = () => <Component key={route.fullPath} />;
46 46
47 const PageContent = unref(openCache) ? ( 47 const PageContent = unref(openCache) ? (
48 - <KeepAlive>{renderComp()}</KeepAlive> 48 + <KeepAlive include={cacheTabs}>{renderComp()}</KeepAlive>
49 ) : ( 49 ) : (
50 renderComp() 50 renderComp()
51 ); 51 );
src/layouts/page/useCache.ts
@@ -35,17 +35,18 @@ export function useCache(isPage: boolean) { @@ -35,17 +35,18 @@ export function useCache(isPage: boolean) {
35 // not parent layout 35 // not parent layout
36 return cached.get(PAGE_LAYOUT_KEY) || []; 36 return cached.get(PAGE_LAYOUT_KEY) || [];
37 } 37 }
38 -  
39 const cacheSet = new Set<string>(); 38 const cacheSet = new Set<string>();
40 cacheSet.add(unref(name)); 39 cacheSet.add(unref(name));
41 40
42 const list = cached.get(unref(name)); 41 const list = cached.get(unref(name));
  42 +
43 if (!list) { 43 if (!list) {
44 return Array.from(cacheSet); 44 return Array.from(cacheSet);
45 } 45 }
46 list.forEach((item) => { 46 list.forEach((item) => {
47 cacheSet.add(item); 47 cacheSet.add(item);
48 }); 48 });
  49 +
49 return Array.from(cacheSet); 50 return Array.from(cacheSet);
50 }); 51 });
51 return { getCaches }; 52 return { getCaches };
src/router/guard/index.ts
1 -import { isNavigationFailure, RouteLocationNormalized, Router } from 'vue-router'; 1 +import { RouteLocationNormalized, Router } from 'vue-router';
2 2
3 import { Modal, notification } from 'ant-design-vue'; 3 import { Modal, notification } from 'ant-design-vue';
4 4
@@ -50,7 +50,7 @@ export function createGuard(router: Router) { @@ -50,7 +50,7 @@ export function createGuard(router: Router) {
50 return true; 50 return true;
51 }); 51 });
52 52
53 - router.afterEach((to, from, failure) => { 53 + router.afterEach((to) => {
54 // scroll top 54 // scroll top
55 isHash((to as RouteLocationNormalized & { href: string })?.href) && body.scrollTo(0, 0); 55 isHash((to as RouteLocationNormalized & { href: string })?.href) && body.scrollTo(0, 0);
56 56
@@ -60,10 +60,6 @@ export function createGuard(router: Router) { @@ -60,10 +60,6 @@ export function createGuard(router: Router) {
60 60
61 // change html title 61 // change html title
62 to.name !== REDIRECT_NAME && setTitle(t(to.meta.title), globSetting.title); 62 to.name !== REDIRECT_NAME && setTitle(t(to.meta.title), globSetting.title);
63 -  
64 - if (isNavigationFailure(failure)) {  
65 - console.error('router navigation failed:', failure);  
66 - }  
67 }); 63 });
68 createPageLoadingGuard(router); 64 createPageLoadingGuard(router);
69 createProgressGuard(router); 65 createProgressGuard(router);
src/router/index.ts
@@ -35,8 +35,8 @@ export function setupRouter(app: App&lt;Element&gt;) { @@ -35,8 +35,8 @@ export function setupRouter(app: App&lt;Element&gt;) {
35 createGuard(router); 35 createGuard(router);
36 } 36 }
37 37
38 -router.onError((error) => {  
39 - console.error(error);  
40 -}); 38 +// router.onError((error) => {
  39 +// console.error(error);
  40 +// });
41 41
42 export default router; 42 export default router;
src/router/routes/modules/demo/level.ts
@@ -55,6 +55,7 @@ const permission: AppRouteModule = { @@ -55,6 +55,7 @@ const permission: AppRouteModule = {
55 component: () => import('/@/views/demo/level/Menu2.vue'), 55 component: () => import('/@/views/demo/level/Menu2.vue'),
56 meta: { 56 meta: {
57 title: 'Menu2', 57 title: 'Menu2',
  58 + // ignoreKeepAlive: true,
58 }, 59 },
59 }, 60 },
60 ], 61 ],
src/store/modules/tab.ts
@@ -97,11 +97,13 @@ class Tab extends VuexModule { @@ -97,11 +97,13 @@ class Tab extends VuexModule {
97 const pageCacheSet = new Set<string>(); 97 const pageCacheSet = new Set<string>();
98 this.tabsState.forEach((tab) => { 98 this.tabsState.forEach((tab) => {
99 const item = getRoute(tab); 99 const item = getRoute(tab);
100 - const needAuth = !item.meta.ignoreAuth; 100 + const needCache = !item.meta.ignoreKeepAlive;
  101 + if (!needCache) return;
  102 +
101 if (item.meta.affix) { 103 if (item.meta.affix) {
102 const name = item.name as string; 104 const name = item.name as string;
103 pageCacheSet.add(name); 105 pageCacheSet.add(name);
104 - } else if (item.matched && needAuth) { 106 + } else if (item.matched && needCache) {
105 const matched = item.matched; 107 const matched = item.matched;
106 const len = matched.length; 108 const len = matched.length;
107 109
@@ -115,7 +117,7 @@ class Tab extends VuexModule { @@ -115,7 +117,7 @@ class Tab extends VuexModule {
115 } 117 }
116 if (i < len - 1) { 118 if (i < len - 1) {
117 const { meta, name } = matched[i + 1]; 119 const { meta, name } = matched[i + 1];
118 - if (meta && (meta.affix || needAuth)) { 120 + if (meta && (meta.affix || needCache)) {
119 const mapList = cacheMap.get(key) || []; 121 const mapList = cacheMap.get(key) || [];
120 if (!mapList.includes(name as string)) { 122 if (!mapList.includes(name as string)) {
121 mapList.push(name as string); 123 mapList.push(name as string);
@@ -210,7 +212,6 @@ class Tab extends VuexModule { @@ -210,7 +212,6 @@ class Tab extends VuexModule {
210 @Mutation 212 @Mutation
211 commitRedoPage() { 213 commitRedoPage() {
212 const route = router.currentRoute.value; 214 const route = router.currentRoute.value;
213 -  
214 for (const [key, value] of this.cachedMapState) { 215 for (const [key, value] of this.cachedMapState) {
215 const index = value.findIndex((item) => item === (route.name as string)); 216 const index = value.findIndex((item) => item === (route.name as string));
216 if (index === -1) { 217 if (index === -1) {