fix: 修复顶栏模式bug和侧栏无图标的bug

This commit is contained in:
pixelmaxQM 2025-04-21 23:14:53 +08:00
parent 09cb29da17
commit 54595d46e5
2 changed files with 35 additions and 46 deletions

View File

@ -5,31 +5,20 @@
height: sideHeight height: sideHeight
}" }"
> >
<template #title> <el-icon v-if="routerInfo.meta.icon">
<div <component :is="routerInfo.meta.icon" />
v-if="!isCollapse" </el-icon>
class="flex items-center" <template v-else>
:style="{ {{ routerInfo.meta.title[0] }}
height: sideHeight </template>
}" <template #title>
> {{ routerInfo.meta.title }}
<el-icon v-if="routerInfo.meta.icon">
<component :is="routerInfo.meta.icon" />
</el-icon>
<span>{{ routerInfo.meta.title }}</span>
</div>
<template v-else>
<el-icon v-if="routerInfo.meta.icon">
<component :is="routerInfo.meta.icon" />
</el-icon>
<span>{{ routerInfo.meta.title }}</span>
</template>
</template> </template>
</el-menu-item> </el-menu-item>
</template> </template>
<script setup> <script setup>
import { computed } from 'vue' import {computed, inject} from 'vue'
import { useAppStore } from '@/pinia' import { useAppStore } from '@/pinia'
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
const appStore = useAppStore() const appStore = useAppStore()

View File

@ -17,8 +17,8 @@
@select="selectTopMenuItem" @select="selectTopMenuItem"
> >
<template v-for="item in routerStore.asyncRouters[0]?.children || []"> <template v-for="item in routerStore.asyncRouters[0]?.children || []">
<el-menu-item <el-menu-item
v-if="!item.hidden && (!item.children || item.children.length === 0)" v-if="!item.hidden && (!item.children || item.children.length === 0)"
:key="item.name" :key="item.name"
:index="item.name" :index="item.name"
class="dark:text-slate-300 overflow-hidden" class="dark:text-slate-300 overflow-hidden"
@ -31,13 +31,13 @@
</el-icon> </el-icon>
<template v-else> <template v-else>
{{ item.meta.title[0] }} {{ item.meta.title[0] }}
</template> </template>
<template #title> <template #title>
{{ item.meta.title }} {{ item.meta.title }}
</template> </template>
</el-menu-item> </el-menu-item>
<template v-else-if="!item.hidden" > <template v-else-if="!item.hidden" >
<el-menu-item <el-menu-item
:key="item.name" :key="item.name"
:index="item.name" :index="item.name"
:class="{'is-active': topActive === item.name}" :class="{'is-active': topActive === item.name}"
@ -110,14 +110,14 @@
import { useRouterStore } from '@/pinia/modules/router' import { useRouterStore } from '@/pinia/modules/router'
import { useAppStore } from '@/pinia' import { useAppStore } from '@/pinia'
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
const appStore = useAppStore() const appStore = useAppStore()
const { device, config } = storeToRefs(appStore) const { device, config } = storeToRefs(appStore)
defineOptions({ defineOptions({
name: 'SidebarMode' name: 'SidebarMode'
}) })
const route = useRoute() const route = useRoute()
const router = useRouter() const router = useRouter()
const routerStore = useRouterStore() const routerStore = useRouterStore()
@ -125,7 +125,7 @@
const active = ref('') const active = ref('')
const topActive = ref('') const topActive = ref('')
const secondLevelMenus = ref([]) const secondLevelMenus = ref([])
const layoutSideWidth = computed(() => { const layoutSideWidth = computed(() => {
if (!isCollapse.value) { if (!isCollapse.value) {
return config.value.layout_side_width return config.value.layout_side_width
@ -136,7 +136,7 @@
provide('isCollapse', isCollapse) provide('isCollapse', isCollapse)
// //
const updateSecondLevelMenus = (menuName) => { const updateSecondLevelMenus = (menuName) => {
const menu = routerStore.asyncRouters[0]?.children.find(item => item.name === menuName) const menu = routerStore.asyncRouters[0]?.children.find(item => item.name === menuName)
@ -144,18 +144,18 @@
secondLevelMenus.value = menu.children secondLevelMenus.value = menu.children
} }
} }
// //
const selectTopMenuItem = (index) => { const selectTopMenuItem = (index) => {
topActive.value = index topActive.value = index
// //
const menu = routerStore.asyncRouters[0]?.children.find(item => item.name === index) const menu = routerStore.asyncRouters[0]?.children.find(item => item.name === index)
// //
if (menu && menu.children && menu.children.length > 0) { if (menu && menu.children && menu.children.length > 0) {
updateSecondLevelMenus(index) updateSecondLevelMenus(index)
// //
const firstVisibleChild = menu.children.find(child => !child.hidden) const firstVisibleChild = menu.children.find(child => !child.hidden)
if (firstVisibleChild) { if (firstVisibleChild) {
@ -166,12 +166,12 @@
navigateToMenuItem(index) navigateToMenuItem(index)
} }
} }
// //
const selectMenuItem = (index) => { const selectMenuItem = (index) => {
navigateToMenuItem(index) navigateToMenuItem(index)
} }
// //
const navigateToMenuItem = (index) => { const navigateToMenuItem = (index) => {
const query = {} const query = {}
@ -208,33 +208,33 @@
} }
watchEffect(() => { watchEffect(() => {
if (route.name === 'Iframe') { if (route.name === 'Iframe') {
active.value = decodeURIComponent(route.query.url) active.value = decodeURIComponent(route.query.url)
return return
} }
active.value = route.meta.activeName || route.name active.value = route.meta.activeName || route.name
// //
const findParentMenu = () => { const findParentMenu = () => {
// //
const isTopMenu = routerStore.asyncRouters[0]?.children.some( const isTopMenu = routerStore.asyncRouters[0]?.children.some(
item => !item.hidden && item.name === route.name item => !item.hidden && item.name === route.name
) )
if (isTopMenu) { if (isTopMenu) {
return route.name return route.name
} }
for (const topMenu of routerStore.asyncRouters[0]?.children || []) { for (const topMenu of routerStore.asyncRouters[0]?.children || []) {
if (topMenu.hidden) continue if (topMenu.hidden) continue
// //
if (topMenu.children && topMenu.children.some(child => child.name === route.name)) { if (topMenu.children && topMenu.children.some(child => child.name === route.name)) {
return topMenu.name return topMenu.name
} }
// //
const checkChildren = (items) => { const checkChildren = (items) => {
for (const item of items || []) { for (const item of items || []) {
@ -247,18 +247,18 @@
} }
return false return false
} }
if (topMenu.children && checkChildren(topMenu.children)) { if (topMenu.children && checkChildren(topMenu.children)) {
return topMenu.name return topMenu.name
} }
} }
return null return null
} }
const parentMenu = findParentMenu() const parentMenu = findParentMenu()
if (parentMenu) { if (parentMenu) {
topActive.value = parentMenu topActive.value = parentMenu
// //
const menu = routerStore.asyncRouters[0]?.children.find(item => item.name === parentMenu) const menu = routerStore.asyncRouters[0]?.children.find(item => item.name === parentMenu)
if (menu && menu.children && menu.children.length > 0) { if (menu && menu.children && menu.children.length > 0) {
@ -269,7 +269,7 @@
const firstMenuWithChildren = routerStore.asyncRouters[0].children.find( const firstMenuWithChildren = routerStore.asyncRouters[0].children.find(
item => !item.hidden && item.children && item.children.length > 0 item => !item.hidden && item.children && item.children.length > 0
) )
if (firstMenuWithChildren) { if (firstMenuWithChildren) {
// //
updateSecondLevelMenus(firstMenuWithChildren.name) updateSecondLevelMenus(firstMenuWithChildren.name)
@ -281,7 +281,7 @@
const firstMenuWithChildren = routerStore.asyncRouters[0].children.find( const firstMenuWithChildren = routerStore.asyncRouters[0].children.find(
item => !item.hidden && item.children && item.children.length > 0 item => !item.hidden && item.children && item.children.length > 0
) )
if (firstMenuWithChildren) { if (firstMenuWithChildren) {
// //
topActive.value = route.name topActive.value = route.name
@ -297,4 +297,4 @@
isCollapse.value = false isCollapse.value = false
} }
}) })
</script> </script>