keepAlive.ts 622 B

1234567891011121314151617181920212223242526
  1. import { defineStore } from 'pinia';
  2. import { ref } from 'vue';
  3. export const useKeepAliveStore = defineStore('keepAlive', () => {
  4. const cachedViews = ref<string[]>([]);
  5. function addCachedView(name: string) {
  6. if (!cachedViews.value.includes(name)) {
  7. cachedViews.value.push(name);
  8. }
  9. }
  10. function removeCachedView(name: string) {
  11. const index = cachedViews.value.indexOf(name);
  12. if (index > -1) {
  13. cachedViews.value.splice(index, 1);
  14. }
  15. }
  16. function clearCachedViews() {
  17. cachedViews.value = [];
  18. }
  19. return { cachedViews, addCachedView, removeCachedView, clearCachedViews };
  20. });