vue3封装Element导航菜单的实例代码

 更新时间:2024年03月30日 09:58:59   作者:永不满足的求知者  
这篇文章主要介绍了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'
    }
  ])

到此这篇关于vue3封装Element导航菜单的文章就介绍到这了,更多相关vue3封装Element导航菜单内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue 简介及基本使用教程

    Vue 简介及基本使用教程

    ​ Vue.js就是一套轻量级的 MVVM 框架,本文通过实例代码给大家介绍Vue 简介及基本使用教程,感兴趣的朋友跟随小编一起看看吧
    2025-10-10
  • 详解VUE 数组更新

    详解VUE 数组更新

    这篇文章主要介绍了VUE 数组更新问题,文中给大家介绍了vue如何监听数据的变化的 ,需要的朋友可以参考下
    2017-12-12
  • Vue-Typed-JS打字动画效果实现全过程

    Vue-Typed-JS打字动画效果实现全过程

    Vue-typed-js是一个用于Vue.js的Typed.js集成插件,它可以创建打字动画效果,这篇文章主要介绍了Vue-Typed-JS打字动画效果实现的相关资料,文中将插件的安装及使用介绍的非常详细,需要的朋友可以参考下
    2025-08-08
  • Vue Element前端应用开发之组织机构和角色管理

    Vue Element前端应用开发之组织机构和角色管理

    本篇文章继续深化Vue Element权限管理模块管理的内容,介绍组织机构和角色管理模块的处理,使得我们了解界面组件化模块的开发思路和做法,提高我们界面设计的技巧,并减少代码的复杂性,提高界面代码的可读性,同时也是利用组件的复用管理。
    2021-05-05
  • Vue组件之非单文件组件用法及说明

    Vue组件之非单文件组件用法及说明

    本文系统介绍了Vue组件的概念、创建与命名规范、嵌套组织、核心原理及最佳实践,强调组件化开发可提升代码复用性和可维护性,并推荐在实际项目中使用单文件组件以优化开发体验和工程化水平
    2025-10-10
  • Nuxt pages下不同的页面对应layout下的页面布局操作

    Nuxt pages下不同的页面对应layout下的页面布局操作

    这篇文章主要介绍了Nuxt pages下不同的页面对应layout下的页面布局操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • 解决Vue项目打包后打开index.html页面显示空白以及图片路径错误的问题

    解决Vue项目打包后打开index.html页面显示空白以及图片路径错误的问题

    这篇文章主要介绍了解决Vue项目打包后打开index.html页面显示空白以及图片路径错误的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • vue日历/日程提醒/html5本地缓存功能

    vue日历/日程提醒/html5本地缓存功能

    这篇文章主要介绍了vue日历/日程提醒/html5本地缓存功能,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • vue 组件 全局注册和局部注册的实现

    vue 组件 全局注册和局部注册的实现

    下面小编就为大家分享一篇vue 组件 全局注册和局部注册的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-02-02
  • 详解vue页面首次加载缓慢原因及解决方案

    详解vue页面首次加载缓慢原因及解决方案

    这篇文章主要介绍了详解vue页面首次加载缓慢原因及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11

最新评论