ry_mall_houtai/ruoyi-ui/src/components/TopNav/index.vue

194 lines
5.8 KiB
Vue
Raw Normal View History

<template>
<el-menu
:default-active="activeMenu"
mode="horizontal"
@select="handleSelect"
>
<template v-for="(item, index) in topMenus">
2023-11-29 12:38:45 +08:00
<el-menu-item :style="{'--theme': theme}" :index="item.path" :key="index" v-if="index < visibleNumber">
<svg-icon
v-if="item.meta && item.meta.icon && item.meta.icon !== '#'"
:icon-class="item.meta.icon"/>
{{ item.meta.title }}
</el-menu-item>
</template>
<!-- 顶部菜单超出数量折叠 -->
<el-submenu :style="{'--theme': theme}" index="more" :key="visibleNumber" v-if="topMenus.length > visibleNumber">
<template slot="title">更多菜单</template>
<template v-for="(item, index) in topMenus">
<el-menu-item
:index="item.path"
:key="index"
2023-11-29 12:38:45 +08:00
v-if="index >= visibleNumber">
<svg-icon
v-if="item.meta && item.meta.icon && item.meta.icon !== '#'"
:icon-class="item.meta.icon"/>
{{ item.meta.title }}
</el-menu-item>
</template>
</el-submenu>
</el-menu>
</template>
<script>
2025-04-27 10:05:51 +08:00
import { constantRoutes } from "@/router"
import { isHttp } from "@/utils/validate"
2022-03-30 10:38:51 +08:00
// 隐藏侧边栏路由
2025-04-27 10:05:51 +08:00
const hideList = ['/index', '/user/profile']
2022-03-30 10:38:51 +08:00
export default {
data() {
return {
// 顶部栏初始数
visibleNumber: 5,
// 当前激活菜单的 index
currentIndex: undefined
2025-04-27 10:05:51 +08:00
}
},
computed: {
2021-04-19 16:59:56 +08:00
theme() {
2025-04-27 10:05:51 +08:00
return this.$store.state.settings.theme
2021-04-19 16:59:56 +08:00
},
// 顶部显示菜单
topMenus() {
2025-04-27 10:05:51 +08:00
let topMenus = []
2021-04-13 09:47:28 +08:00
this.routers.map((menu) => {
2021-04-13 14:49:42 +08:00
if (menu.hidden !== true) {
// 兼容顶部栏一级菜单内部跳转
2025-04-22 12:03:31 +08:00
if (menu.path === '/' && menu.children) {
2025-04-27 10:05:51 +08:00
topMenus.push(menu.children[0])
} else {
2025-04-27 10:05:51 +08:00
topMenus.push(menu)
}
2021-04-13 09:47:28 +08:00
}
2025-04-27 10:05:51 +08:00
})
return topMenus
},
// 所有的路由信息
routers() {
2025-04-27 10:05:51 +08:00
return this.$store.state.permission.topbarRouters
},
// 设置子路由
childrenMenus() {
2025-04-27 10:05:51 +08:00
var childrenMenus = []
this.routers.map((router) => {
for (var item in router.children) {
if (router.children[item].parentPath === undefined) {
if(router.path === "/") {
2025-04-27 10:05:51 +08:00
router.children[item].path = "/" + router.children[item].path
} else {
2024-11-20 10:42:41 +08:00
if(!isHttp(router.children[item].path)) {
2025-04-27 10:05:51 +08:00
router.children[item].path = router.path + "/" + router.children[item].path
2021-07-08 19:17:09 +08:00
}
}
2025-04-27 10:05:51 +08:00
router.children[item].parentPath = router.path
}
2025-04-27 10:05:51 +08:00
childrenMenus.push(router.children[item])
}
2025-04-27 10:05:51 +08:00
})
return constantRoutes.concat(childrenMenus)
},
// 默认激活的菜单
activeMenu() {
2025-04-27 10:05:51 +08:00
const path = this.$route.path
let activePath = path
2022-03-30 10:38:51 +08:00
if (path !== undefined && path.lastIndexOf("/") > 0 && hideList.indexOf(path) === -1) {
2025-04-27 10:05:51 +08:00
const tmpPath = path.substring(1, path.length)
if (!this.$route.meta.link) {
2025-04-27 10:05:51 +08:00
activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"))
this.$store.dispatch('app/toggleSideBarHide', false)
}
} else if(!this.$route.children) {
2025-04-27 10:05:51 +08:00
activePath = path
this.$store.dispatch('app/toggleSideBarHide', true)
}
2025-04-27 10:05:51 +08:00
this.activeRoutes(activePath)
return activePath
},
},
beforeMount() {
window.addEventListener('resize', this.setVisibleNumber)
},
beforeDestroy() {
window.removeEventListener('resize', this.setVisibleNumber)
},
mounted() {
2025-04-27 10:05:51 +08:00
this.setVisibleNumber()
},
methods: {
// 根据宽度计算设置显示栏数
setVisibleNumber() {
2025-04-27 10:05:51 +08:00
const width = document.body.getBoundingClientRect().width / 3
this.visibleNumber = parseInt(width / 85)
},
// 菜单选择事件
handleSelect(key, keyPath) {
2025-04-27 10:05:51 +08:00
this.currentIndex = key
const route = this.routers.find(item => item.path === key)
2024-11-20 10:42:41 +08:00
if (isHttp(key)) {
// http(s):// 路径新窗口打开
2025-04-27 10:05:51 +08:00
window.open(key, "_blank")
} else if (!route || !route.children) {
// 没有子路由路径内部打开
2025-04-27 10:05:51 +08:00
const routeMenu = this.childrenMenus.find(item => item.path === key)
if (routeMenu && routeMenu.query) {
2025-04-27 10:05:51 +08:00
let query = JSON.parse(routeMenu.query)
this.$router.push({ path: key, query: query })
} else {
2025-04-27 10:05:51 +08:00
this.$router.push({ path: key })
}
2025-04-27 10:05:51 +08:00
this.$store.dispatch('app/toggleSideBarHide', true)
} else {
// 显示左侧联动菜单
2025-04-27 10:05:51 +08:00
this.activeRoutes(key)
this.$store.dispatch('app/toggleSideBarHide', false)
}
},
// 当前激活的路由
activeRoutes(key) {
2025-04-27 10:05:51 +08:00
var routes = []
if (this.childrenMenus && this.childrenMenus.length > 0) {
this.childrenMenus.map((item) => {
if (key == item.parentPath || (key == "index" && "" == item.path)) {
2025-04-27 10:05:51 +08:00
routes.push(item)
}
2025-04-27 10:05:51 +08:00
})
}
if(routes.length > 0) {
2025-04-27 10:05:51 +08:00
this.$store.commit("SET_SIDEBAR_ROUTERS", routes)
} else {
2025-04-27 10:05:51 +08:00
this.$store.dispatch('app/toggleSideBarHide', true)
}
2021-04-13 09:47:28 +08:00
}
},
2025-04-27 10:05:51 +08:00
}
</script>
2021-04-19 16:59:56 +08:00
<style lang="scss">
2021-07-25 19:06:57 +08:00
.topmenu-container.el-menu--horizontal > .el-menu-item {
float: left;
2021-07-25 19:06:57 +08:00
height: 50px !important;
line-height: 50px !important;
color: #999093 !important;
padding: 0 5px !important;
margin: 0 10px !important;
}
2021-07-25 19:06:57 +08:00
.topmenu-container.el-menu--horizontal > .el-menu-item.is-active, .el-menu--horizontal > .el-submenu.is-active .el-submenu__title {
border-bottom: 2px solid #{'var(--theme)'} !important;
color: #303133;
}
2021-04-19 16:59:56 +08:00
/* submenu item */
2021-07-25 19:06:57 +08:00
.topmenu-container.el-menu--horizontal > .el-submenu .el-submenu__title {
float: left;
height: 50px !important;
line-height: 50px !important;
color: #999093 !important;
padding: 0 5px !important;
margin: 0 10px !important;
2021-04-19 16:59:56 +08:00
}
</style>