forked from un-pany/mobvue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeep-alive.ts
29 lines (24 loc) · 901 Bytes
/
keep-alive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import type { RouteLocationNormalizedGeneric } from "vue-router"
import { pinia } from "@/pinia"
import { isString } from "@@/utils/validate"
export const useKeepAliveStore = defineStore("keep-alive", () => {
const cachedRoutes = ref<string[]>([])
const addCachedRoute = (route: RouteLocationNormalizedGeneric) => {
const keepAlive = route.meta.keepAlive
const name = route.name
if (keepAlive && name && isString(name) && !cachedRoutes.value.includes(name)) {
cachedRoutes.value.push(name)
}
}
const delAllCachedRoutes = () => {
cachedRoutes.value = []
}
return { cachedRoutes, addCachedRoute, delAllCachedRoutes }
})
/**
* @description 在 SPA 应用中可用于在 pinia 实例被激活前使用 store
* @description 在 SSR 应用中可用于在 setup 外使用 store
*/
export function useKeepAliveStoreOutside() {
return useKeepAliveStore(pinia)
}