当前位置: 首页 > news >正文

vue3封装Element导航菜单

请添加图片描述

1. 导航外层布局 AsideView.vue

<template><el-menu:default-active="defaultActive"class="my-menu":collapse="isCollapse":collapse-transition="false"@open="handleOpen"@close="handleClose"><menu-item :menuList="menuList"></menu-item></el-menu>
</template><script lang="ts" setup>
import { useRoute } from "vue-router";import MenuItem from "./components/MenuItem.vue";
const collapseStore = useCollapseStore();
const isCollapse = computed(() => collapseStore.collapse);
const store = useMenuStore();
const menuList = store.menuList;
const flattenMenuList = store.flattenMenuList();
const defaultActive = ref("");
onMounted(() => {const route = useRoute();watch(() => route.fullPath,(newPath, oldPath) => {getDefaultActive(newPath);},{immediate: true,});
});const getDefaultActive = (path) => {const currentMenu = flattenMenuList.find((item) => item.path === path);if (currentMenu) {defaultActive.value = currentMenu.id;}console.log("defaultActive", defaultActive.value);
};const handleOpen = (key: string, keyPath: string[]) => {console.log(key, keyPath);
};
const handleClose = (key: string, keyPath: string[]) => {console.log(key, keyPath);
};
</script><style lang="scss">
.icon-collapse {cursor: pointer;width: 64px;height: 30px;margin: 0 auto;
}
.my-menu {background-color: #545c64;border-right: none;color: #fff;height: 100%;overflow-x: hidden;overflow-y: auto;
}
.el-menu-item,
.el-sub-menu__title {background-color: #545c64;color: #fff;
}
.el-menu-item:hover,
.el-sub-menu__title:hover {background: rgb(62, 64, 74);
}
.el-menu-item.is-active,
.el-sub-menu__title.is-active {background: rgb(62, 64, 74);
}
/* 滚动条 */
::-webkit-scrollbar {/*滚动条整体样式*/width: 7px; /*高宽分别对应横竖滚动条的尺寸*/height: 7px;&-thumb {/*滚动条里面小方块*/border-radius: 7px;background-color: #d0d0d0;&:hover {/*滚动条里面小方块*/background-color: #b7b7b7;}}&-track {/*滚动条里面轨道*/border-radius: 7px;background-color: #fff;}
}
</style>

2. 单个导航菜单封装 MenuItem.vue

<template><template v-for="item in menuList" :key="item.id"><el-sub-menu:index="item.id"v-if="item.children && item.children.length > 0"><template #title><el-icon :size="30"><component class="fold-menu" :is="item.icon"></component></el-icon><span>{{ item.name }}</span></template><menu-item :menuList="item.children"></menu-item></el-sub-menu><el-menu-item :index="item.id" v-else @click="handleJump(item)"><el-icon :size="30"><component class="fold-menu" :is="item.icon"></component></el-icon><template #title>{{ item.name }}</template></el-menu-item></template>
</template>
<script setup lang="ts">
import { useRouter } from "vue-router";
import type { MenuInfo } from "~/types/menu";
const router = useRouter();const props = defineProps({menuList: {type: Array<MenuInfo>,},
});const handleJump = (item: MenuInfo) => {if (item.path) {router.push(item.path);}
};
</script><style lang="scss" scoped></style>

3. 控制导航收缩 stores/collapse.ts

import { ref } from 'vue'
import { defineStore } from 'pinia'export const useCollapseStore = defineStore('collapse', () => {const collapse = ref(false)const changeCollapse = function changeCollapse() {collapse.value = !collapse.value}return { collapse, changeCollapse }
})

4. 菜单数据格式示例

  const menuList = ref<MenuInfo[]>([{id: '1',name: '首页',path: '/',icon: 'HomeFilled',},{id: '2',name: '用户管理',path: '/user-manage',icon: 'UserFilled',},{id: '3',name: '权限管理',path: '/permission',icon: 'Lock',},{id: '4',name: '商品管理',path: '/product',icon: 'ShoppingBag',children: [{id: '4-1',name: '商品列表',path: '/product/list',icon: 'ShoppingBag'}]},{id: '5',name: '订单管理',path: '/order',icon: 'Document',children: [{id: '5-1',name: '订单列表',path: '/order/list',icon: 'Document'}]},{id: '6',name: '数据统计',path: '/data',icon: 'DataAnalysis'},{id: '7',name: '系统设置',path: '/system',icon: 'Setting'},{id: '8',name: '其他',path: '/other',icon: 'Document'}])
http://www.lryc.cn/news/329703.html

相关文章:

  • 字符串的函数
  • Linux安装redis(基于CentOS系统,Ubuntu也可参考)
  • ChatGPT引领量化交易革命:AI在金融创新的浪潮中崭露头角
  • 无忧微服务:如何实现大流量下新版本的发布自由
  • Halcon3D表面平面度检测-平面差值法
  • golang 在多线程中避免 CPU 指令重排
  • 自动化更新包文件--shell脚本
  • Vue element-plus 导航栏 [el-menu]
  • 数据结构——数组
  • python asyncio websockets server
  • 视频素材免费网站有哪些?8个视频素材库网站下载推荐
  • ChatGPT与传统搜索引擎的区别:智能对话与关键词匹配的差异
  • xargs后调用bash自定义函数(写该函数文本到脚本, 并引导PATH)
  • 学术论文写作新利器:ChatGPT技巧详解
  • Spring整合JDBC
  • 详解Qt中的布局管理器
  • MyBatis 参数重复打印的bug
  • ES6学习之路:迭代器Iterator和生成器Generator
  • 如何使用 DynamiCrafter Interp Loop 无缝连接两张照片
  • 今天起,Windows可以一键召唤GPT-4了
  • 使用Kaggle API快速下载Kaggle数据集
  • java 通过 microsoft graph 调用outlook(二)
  • 【机器学习】代价函数
  • [leetcode] 100. 相同的树
  • 08、Lua 函数
  • 【数据分析面试】1. 计算年度收入百分比(SQL)
  • 数据库SQL语句速查手册
  • 智慧城市一屏统览,数字孪生综合治理
  • Python读取PDF文字转txt,解决分栏识别问题,能读两栏
  • 微信支付平台与微信服务号关联配置要点