|
| 1 | +import { defineComponent, ref, watch, computed } from 'vue' |
| 2 | +import { useRoute, useRouter, RouteRecordRaw } from 'vue-router' |
| 3 | +import { Menu } from 'ant-design-vue' |
| 4 | + |
| 5 | +import styles from './index.module.less' |
| 6 | + |
| 7 | +const Menus = defineComponent({ |
| 8 | + name: 'Menus', |
| 9 | + props: { |
| 10 | + mode: { |
| 11 | + type: String, |
| 12 | + default: 'horizontal', |
| 13 | + }, |
| 14 | + }, |
| 15 | + setup(props, { slots }) { |
| 16 | + const route = useRoute() |
| 17 | + const activeRoute = ref(route.path) // 当前路由 |
| 18 | + |
| 19 | + const openKeys = computed(() => { |
| 20 | + if (props.mode == 'inline') { |
| 21 | + return ['/smart', '/eoms', '/system', '/database'] |
| 22 | + } |
| 23 | + return [] |
| 24 | + }) // 默认展开项 |
| 25 | + |
| 26 | + const menuLists = computed(() => { |
| 27 | + return getMenus().filter((item) => !item?.meta?.hidden) |
| 28 | + }) // 获取显示状态的路由 |
| 29 | + |
| 30 | + const getMenus = () => { |
| 31 | + let menuList: RouteRecordRaw[] = [] |
| 32 | + const routes: Array<RouteRecordRaw> = useRouter().options?.routes || [] |
| 33 | + routes.forEach((item) => { |
| 34 | + if (item.path == '/') { |
| 35 | + menuList = item.children || [] |
| 36 | + } |
| 37 | + }) |
| 38 | + return menuList |
| 39 | + } // 获取路由列表 |
| 40 | + |
| 41 | + watch(route, (val) => { |
| 42 | + activeRoute.value = val.path |
| 43 | + }) // 监听路由变化 |
| 44 | + |
| 45 | + return () => ( |
| 46 | + <div |
| 47 | + class={`${styles['menu-class']} ${styles[props.mode + '-menu-class']}`} |
| 48 | + > |
| 49 | + <Menu |
| 50 | + theme="dark" |
| 51 | + selectedKeys={[activeRoute.value]} |
| 52 | + openKeys={openKeys.value} |
| 53 | + style={{ lineHeight: '54px' }} |
| 54 | + > |
| 55 | + {menuLists['value'].map((menu) => { |
| 56 | + if (!menu.children || !menu.children.length) { |
| 57 | + return ( |
| 58 | + <Menu.Item key={menu.path}> |
| 59 | + {menu.meta?.icon && ( |
| 60 | + <span class={`iconfont ${menu.meta.icon} menu-icon`}></span> |
| 61 | + )} |
| 62 | + <span>{menu.meta?.title}</span> |
| 63 | + </Menu.Item> |
| 64 | + ) |
| 65 | + } |
| 66 | + })} |
| 67 | + </Menu> |
| 68 | + </div> |
| 69 | + ) |
| 70 | + }, |
| 71 | +}) |
| 72 | + |
| 73 | +export default Menus |
0 commit comments