refactor: 布局代码优化

This commit is contained in:
hxr
2024-02-10 09:33:57 +08:00
parent 56f5ac3802
commit 6b5c3a7f2a
3 changed files with 13 additions and 17 deletions

View File

@@ -32,16 +32,16 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { RouteRecordRaw } from "vue-router";
import { usePermissionStore, useAppStore } from "@/store"; import { usePermissionStore, useAppStore } from "@/store";
import { translateRouteTitle } from "@/utils/i18n"; import { translateRouteTitle } from "@/utils/i18n";
import variables from "@/styles/variables.module.scss"; import variables from "@/styles/variables.module.scss";
import { RouteRecordRaw } from "vue-router";
const appStore = useAppStore(); const appStore = useAppStore();
const permissionStore = usePermissionStore(); const permissionStore = usePermissionStore();
const router = useRouter(); const router = useRouter();
const activePath = computed(() => appStore.activeTopMenu); const activePath = computed(() => appStore.activeTopMenuPath);
// 顶部菜单集合 // 顶部菜单集合
const mixTopMenus = ref<RouteRecordRaw[]>([]); const mixTopMenus = ref<RouteRecordRaw[]>([]);
@@ -49,10 +49,10 @@ const mixTopMenus = ref<RouteRecordRaw[]>([]);
/** /**
* 菜单选择事件 * 菜单选择事件
*/ */
const handleMenuSelect = (selectedRoutePath: string) => { const handleMenuSelect = (routePath: string) => {
appStore.activeTopMenu(selectedRoutePath); appStore.activeTopMenu(routePath);
permissionStore.setMixLeftMenus(selectedRoutePath); permissionStore.setMixLeftMenus(routePath);
// 获取左侧菜单集合,跳转到第一个菜单 // 获取左侧菜单集合,默认跳转到第一个菜单
const mixLeftMenus = permissionStore.mixLeftMenus; const mixLeftMenus = permissionStore.mixLeftMenus;
goToFirstMenu(mixLeftMenus); goToFirstMenu(mixLeftMenus);
}; };

View File

@@ -12,7 +12,7 @@
<!-- 混合布局 --> <!-- 混合布局 -->
<div v-if="layout === 'mix'" class="mix-container"> <div v-if="layout === 'mix'" class="mix-container">
<div class="mix-container__left"> <div class="mix-container__left">
<SidebarMenu :menu-list="mixLeftMenus" :base-path="activeTopMenu" /> <SidebarMenu :menu-list="mixLeftMenus" :base-path="activeTopMenuPath" />
<div class="sidebar-toggle"> <div class="sidebar-toggle">
<hamburger <hamburger
:is-active="appStore.sidebar.opened" :is-active="appStore.sidebar.opened"
@@ -35,7 +35,7 @@
<!-- 左侧布局|| 顶部布局 --> <!-- 左侧布局|| 顶部布局 -->
<div v-else :class="{ hasTagsView: showTagsView }" class="main-container"> <div v-else :class="{ hasTagsView: showTagsView }" class="main-container">
<div :class="{ 'fixed-header': fixedHeader }"> <div :class="{ 'fixed-header': fixedHeader }">
<Navbar v-if="layout === 'left'" /> <NavBar v-if="layout === 'left'" />
<TagsView v-if="showTagsView" /> <TagsView v-if="showTagsView" />
</div> </div>
<AppMain /> <AppMain />
@@ -61,8 +61,8 @@ const showTagsView = computed(() => settingsStore.tagsView);
const showSettings = computed(() => settingsStore.showSettings); const showSettings = computed(() => settingsStore.showSettings);
const layout = computed(() => settingsStore.layout); const layout = computed(() => settingsStore.layout);
const activeTopMenu = computed(() => { const activeTopMenuPath = computed(() => {
return appStore.activeTopMenu; return appStore.activeTopMenuPath;
}); });
// 混合模式左侧菜单 // 混合模式左侧菜单
const mixLeftMenus = computed(() => { const mixLeftMenus = computed(() => {
@@ -70,7 +70,7 @@ const mixLeftMenus = computed(() => {
}); });
watch( watch(
() => activeTopMenu.value, () => activeTopMenuPath.value,
(newVal) => { (newVal) => {
if (layout.value !== "mix") return; if (layout.value !== "mix") return;
permissionStore.setMixLeftMenus(newVal); permissionStore.setMixLeftMenus(newVal);

View File

@@ -1,5 +1,4 @@
import { RouteRecordRaw } from "vue-router"; import { RouteRecordRaw } from "vue-router";
import { defineStore } from "pinia";
import { constantRoutes } from "@/router"; import { constantRoutes } from "@/router";
import { store } from "@/store"; import { store } from "@/store";
import { listRoutes } from "@/api/menu"; import { listRoutes } from "@/api/menu";
@@ -98,15 +97,12 @@ export const usePermissionStore = defineStore("permission", () => {
}); });
}); });
} }
/** /**
* 获取与激活的顶部菜单项相关的混合模式左侧菜单集合 * 获取与激活的顶部菜单项相关的混合模式左侧菜单集合
*/ */
const mixLeftMenus = ref<RouteRecordRaw[]>([]); const mixLeftMenus = ref<RouteRecordRaw[]>([]);
function setMixLeftMenus(activeTopMenu: string) { function setMixLeftMenus(topMenuPath: string) {
const matchedItem = routes.value.find( const matchedItem = routes.value.find((item) => item.path === topMenuPath);
(item) => item.path === activeTopMenu
);
if (matchedItem && matchedItem.children) { if (matchedItem && matchedItem.children) {
mixLeftMenus.value = matchedItem.children; mixLeftMenus.value = matchedItem.children;
} }