Merge pull request #194 from taisha/main

fix(components): refresh cached routes
This commit is contained in:
Soybean 2023-03-02 08:20:41 +08:00 committed by GitHub
commit c3d0b74c75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -5,18 +5,27 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { useAppStore } from '@/store'; import { useRoute } from 'vue-router';
import { useAppStore, useRouteStore } from '@/store';
import { useLoading } from '@/hooks'; import { useLoading } from '@/hooks';
defineOptions({ name: 'ReloadButton' }); defineOptions({ name: 'ReloadButton' });
const app = useAppStore(); const app = useAppStore();
const routeStore = useRouteStore();
const route = useRoute();
const { loading, startLoading, endLoading } = useLoading(); const { loading, startLoading, endLoading } = useLoading();
function handleRefresh() { function handleRefresh() {
const isCached = routeStore.cacheRoutes.includes(String(route.name));
if (isCached) {
routeStore.removeCacheRoute(route.name as AuthRoute.AllRouteKey);
}
startLoading(); startLoading();
app.reloadPage(); app.reloadPage();
setTimeout(() => { setTimeout(() => {
if (isCached) {
routeStore.addCacheRoute(route.name as AuthRoute.AllRouteKey);
}
endLoading(); endLoading();
}, 1000); }, 1000);
} }

View File

@ -146,6 +146,22 @@ export const useRouteStore = defineStore('route-store', {
} else { } else {
await this.initStaticRoute(); await this.initStaticRoute();
} }
},
/** 从缓存路由中去除某个路由 */
removeCacheRoute(name: AuthRoute.AllRouteKey) {
const index = this.cacheRoutes.indexOf(name);
if (index > -1) {
this.cacheRoutes.splice(index, 1);
}
},
/** 添加某个缓存路由 */
addCacheRoute(name: AuthRoute.AllRouteKey) {
const index = this.cacheRoutes.indexOf(name);
if (index === -1) {
this.cacheRoutes.push(name);
}
} }
} }
}); });