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