zhengyiming
2025-02-10 958b79ed89b9e742540f714a80261d222c0fc09b
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { RouteLocationNormalizedLoaded, RouteLocationNamedRaw } from 'vue-router';
import { useTagsViewStoreHook } from '@/store/modules/tagsView';
 
export function useRouteView() {
  const tagsViewStore = useTagsViewStoreHook();
 
  const router = useRouter();
  const route = useRoute();
 
  const delView = (view: RouteLocationNormalizedLoaded) => {
    tagsViewStore.delViewSync(view);
  };
 
  const refreshView = async (view: RouteLocationNormalizedLoaded) => {
    await tagsViewStore.delCachedView(view);
    const { fullPath } = view;
    await nextTick();
    router.replace({
      path: '/redirect' + fullPath,
    });
  };
 
  /**
   * 跳转路由并清除该目标路由在tagsView的缓存
   */
  function goViewByClearCache(to: RouteLocationNamedRaw) {
    tagsViewStore.delCachedView({
      name: to.name,
    });
    router.push(to);
  }
 
  function closeViewPush(view: RouteLocationNormalizedLoaded, to: RouteLocationNamedRaw) {
    delView(view);
    router.push(to);
  }
 
  function closeToLastView() {
    delView(route);
    const latestView = tagsViewStore.visitedViews.slice(-1)[0];
    if (latestView) {
      router.push(latestView.fullPath);
    } else {
      if (route && route.name === 'Dashboard') {
        router.replace({ path: '/redirect' + route.fullPath });
      } else {
        router.push('/');
      }
    }
  }
 
  return { delView, refreshView, goViewByClearCache, closeViewPush, closeToLastView };
}