header-mix-nav-menus.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <script lang="ts">
  2. import { EMitt, ESidebarLayoutEnum, EThemeSetting } from "@/constants/enum";
  3. import emits from "@/utils/emits";
  4. import { getThemeConfigCacheByKey } from "@/utils/theme";
  5. import { getValueByKeys } from "@/utils/utils";
  6. import { computed, defineComponent, reactive, watch } from "vue";
  7. import { RouteRecordRaw, useRoute, useRouter } from "vue-router";
  8. import { useAppStore } from "@/store";
  9. import BaseSidebar from "../sidebar/base-sidebar.vue";
  10. /**
  11. * 顶部导航菜单,混合布局模式下用到
  12. */
  13. export default defineComponent({
  14. name: "HeaderMixNavMenus",
  15. components: { BaseSidebar },
  16. setup() {
  17. const store = useAppStore();
  18. const router = useRouter();
  19. const route = useRoute();
  20. const routers = router.options.routes;
  21. const state = reactive({
  22. currRoute: getValueByKeys(getValueByKeys(router.currentRoute.value.meta, "matched", [])[0], "path", "")
  23. });
  24. watch(
  25. () => route.path,
  26. () => {
  27. if (getThemeConfigCacheByKey(EThemeSetting.NavLayout) === ESidebarLayoutEnum.Mix) {
  28. const matchedRoute = getValueByKeys(getValueByKeys(router.currentRoute.value.meta, "matched", [])[0], "path", "");
  29. if (matchedRoute) {
  30. state.currRoute = matchedRoute;
  31. emits.emit(EMitt.OnSelectHeaderNavMenusByMixNav, matchedRoute);
  32. }
  33. }
  34. }
  35. );
  36. const topHeaderMenus = computed(() => {
  37. const rs: any[] = [];
  38. store.state.routes.forEach((x: RouteRecordRaw) => {
  39. rs.push({
  40. path: x.path,
  41. children: [],
  42. meta: x.meta ? x.meta : {}
  43. });
  44. });
  45. return rs;
  46. });
  47. const onSelect = (path: string) => {
  48. const curr = routers.find((x: RouteRecordRaw) => x.path === path);
  49. if (!curr?.children?.length) {
  50. router.push(path);
  51. } else {
  52. state.currRoute = path;
  53. emits.emit(EMitt.OnSelectHeaderNavMenusByMixNav, path);
  54. }
  55. };
  56. return { state, topHeaderMenus, onSelect };
  57. }
  58. });
  59. </script>
  60. <template>
  61. <base-sidebar mode="horizontal" :menus="topHeaderMenus" :router="false" :currRoute="state.currRoute" :is-mobile="false" :onSelect="onSelect"></base-sidebar>
  62. </template>