import { onBeforeRouteLeave } from "vue-router";
import { useKeepAliveStore } from "@/stores/modules/keepAlive";
/**
 * @description 页面离开清除缓存，一般在详情页使用
 * @param name 菜单组件名称 (一般为页面组件 name)
 */
export const usePageLeave = (name: string) => {
  const keepAlive = useKeepAliveStore();
  onBeforeRouteLeave((to, from) => {
    const { activeMenu } = from.meta || {};
    // 只有跳转到详情页，才缓存页面
    if (activeMenu && to.path !== activeMenu) {
      keepAlive.removeKeepAliveName(name);
    }
  });
};
