Ver código fonte

🎨 修复重新登录后列表未刷新问题

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
快乐的梦鱼 2 semanas atrás
pai
commit
b84b70c813
5 arquivos alterados com 41 adições e 4 exclusões
  1. 4 3
      src/App.vue
  2. 1 0
      src/pages/admin.vue
  3. 8 0
      src/router/index.ts
  4. 3 1
      src/stores/auth.ts
  5. 25 0
      src/stores/keepAlive.ts

+ 4 - 3
src/App.vue

@@ -11,10 +11,9 @@
     <NavBar v-if="!isInMiniProgram" />
     <main>
       <RouterView v-slot="{ Component }">
-        <KeepAlive>
-          <component :is="Component" v-if="route.meta.keepAlive" />
+        <KeepAlive :include="keepAliveStore.cachedViews">
+          <component :is="Component" :key="route.name" />
         </KeepAlive>
-        <component :is="Component" v-if="!route.meta.keepAlive" />
       </RouterView>
     </main>
     <FooterSmall v-if="!isInMiniProgram" />
@@ -25,6 +24,7 @@
 import { onMounted, watch } from 'vue';
 import { RouterView, useRoute } from 'vue-router'
 import { useAuthStore } from './stores/auth';
+import { useKeepAliveStore } from './stores/keepAlive';
 import { useRedirectLoginPage } from './common/LoginPageRedirect';
 import { useAppConfiguration } from './api/system/useAppConfiguration';
 import NavBar from './components/NavBar.vue';
@@ -33,6 +33,7 @@ import FooterSmall from './components/FooterSmall.vue';
 import { isInMiniProgram } from './composeables/MiniProgramIng.ts';
 
 const authStore = useAuthStore();
+const keepAliveStore = useKeepAliveStore();
 const { checkAndRedirectLoginPage } = useRedirectLoginPage();
 const { loadAppConfiguration } = useAppConfiguration();
 

+ 1 - 0
src/pages/admin.vue

@@ -159,6 +159,7 @@
 </template>
 
 <script setup lang="ts">
+defineOptions({ name: 'Admin' });
 import { computed, onMounted, ref, watch } from 'vue';
 import { useRoute, useRouter } from 'vue-router';
 import { useAuthStore } from '@/stores/auth';

+ 8 - 0
src/router/index.ts

@@ -1,4 +1,5 @@
 import { createRouter, createWebHashHistory } from 'vue-router'
+import { useKeepAliveStore } from '@/stores/keepAlive'
 import Index from '@/pages/index.vue'
 import Test from '@/pages/test.vue'
 import NotFound from '@/pages/404.vue'
@@ -102,4 +103,11 @@ const router = createRouter({
   ],
 })
 
+router.beforeEach((to) => {
+  if (to.meta.keepAlive && to.name) {
+    const keepAliveStore = useKeepAliveStore();
+    keepAliveStore.addCachedView(to.name as string);
+  }
+});
+
 export default router

+ 3 - 1
src/stores/auth.ts

@@ -1,6 +1,7 @@
 import UserApi, { LoginResult, UserInfo } from "@/api/auth/UserApi";
 import { HtmlUtils, SettingsUtils } from "@imengyu/imengyu-utils";
 import { defineStore } from "pinia"
+import { useKeepAliveStore } from "./keepAlive";
 
 const STORAGE_KEY = 'authInfo';
 const canRefresh = false;
@@ -79,7 +80,8 @@ export const useAuthStore = defineStore('auth', {
       this.token = '';
       this.userId = 0;
       this.userInfo = null;
-
+      const keepAliveStore = useKeepAliveStore();
+      keepAliveStore.removeCachedView('Admin');
       SettingsUtils.setSettings('inheritorShowInMessageLastTime', 0);
       localStorage.removeItem(STORAGE_KEY);
     },

+ 25 - 0
src/stores/keepAlive.ts

@@ -0,0 +1,25 @@
+import { defineStore } from 'pinia';
+import { ref } from 'vue';
+
+export const useKeepAliveStore = defineStore('keepAlive', () => {
+  const cachedViews = ref<string[]>([]);
+
+  function addCachedView(name: string) {
+    if (!cachedViews.value.includes(name)) {
+      cachedViews.value.push(name);
+    }
+  }
+
+  function removeCachedView(name: string) {
+    const index = cachedViews.value.indexOf(name);
+    if (index > -1) {
+      cachedViews.value.splice(index, 1);
+    }
+  }
+
+  function clearCachedViews() {
+    cachedViews.value = [];
+  }
+
+  return { cachedViews, addCachedView, removeCachedView, clearCachedViews };
+});