vue3结合element-plus封装ems菜单栏组件

效果如下:

在这里插入图片描述

ok,开始代码

nav-menu.vue

<template>
  <div class="nav-menu">
    <div class="logo">
      <img class="img" src="~@/assets/img/logo.svg" alt="logo" />
      <span v-if="!collapse" class="title">Vue3+TS</span>
    </div>
    <el-menu
      :default-active="defaultValue"
      class="el-menu-vertical"
      :collapse="collapse"
      background-color="#0c2135"
      text-color="#b7bdc3"
      active-text-color="#0a60bd"
    >
      <template v-for="item in userMenus" :key="item.id">
        <!-- 二级菜单 -->
        <template v-if="item.type === 1">
          <!-- 二级菜单的可以展开的标题 -->
          <el-submenu :index="item.id + ''">
            <template #title>
              <i v-if="item.icon" :class="item.icon"></i>
              <span>{{ item.name }}</span>
            </template>
            <!-- 遍历里面的item -->
            <template v-for="subitem in item.children" :key="subitem.id">
              <el-menu-item
                :index="subitem.id + ''"
                @click="handleMenuItemClick(subitem)"
              >
                <i v-if="subitem.icon" :class="subitem.icon"></i>
                <span>{{ subitem.name }}</span>
              </el-menu-item>
            </template>
          </el-submenu>
        </template>
        <!-- 一级菜单 -->
        <template v-else-if="item.type === 2">
          <el-menu-item :index="item.id + ''">
            <i v-if="item.icon" :class="item.icon"></i>
            <span>{{ item.name }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref } from 'vue'
import { useStore } from '@/store'
import { useRouter, useRoute } from 'vue-router'

import { pathMapToMenu } from '@/utils/map-menus'

// vuex - typescript  => pinia

export default defineComponent({
  props: {
    collapse: {
      type: Boolean,
      default: false
    }
  },
  setup() {
    // store
    const store = useStore()
    const userMenus = computed(() => store.state.login.userMenus)

    // router
    const router = useRouter()
    const route = useRoute()
    const currentPath = route.path

    // data
    const menu = pathMapToMenu(userMenus.value, currentPath)
    const defaultValue = ref(menu.id + '')

    // event handle
    const handleMenuItemClick = (item: any) => {
      router.push({
        path: item.url ?? '/not-found'
      })
    }
    return {
      userMenus,
      defaultValue,
      handleMenuItemClick
    }
  }
})
</script>

<style scoped lang="less">
.nav-menu {
  height: 100%;
  background-color: #001529;

  .logo {
    display: flex;
    height: 28px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;

    .img {
      height: 100%;
      margin: 0 10px;
    }

    .title {
      font-size: 16px;
      font-weight: 700;
      color: white;
    }
  }

  .el-menu {
    border-right: none;
  }

  // 目录
  .el-submenu {
    background-color: #001529 !important;
    // 二级菜单 ( 默认背景 )
    .el-menu-item {
      padding-left: 50px !important;
      background-color: #0c2135 !important;
    }
  }

  ::v-deep .el-submenu__title {
    background-color: #001529 !important;
  }

  // hover 高亮
  .el-menu-item:hover {
    color: #fff !important; // 菜单
  }

  .el-menu-item.is-active {
    color: #fff !important;
    background-color: #0a60bd !important;
  }
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 100%;
  height: calc(100% - 48px);
}
</style>

userMenus是后台请求回来的菜单栏接口数据

在这里插入图片描述
pathMapToMenu方法

export function pathMapToMenu(
  userMenus: any[],
  currentPath: string,
  breadcrumbs?: IBreadcrumb[]
): any {
  for (const menu of userMenus) {
    if (menu.type === 1) {
      const findMenu = pathMapToMenu(menu.children ?? [], currentPath)
      if (findMenu) {
        breadcrumbs?.push({ name: menu.name })
        breadcrumbs?.push({ name: findMenu.name })
        return findMenu
      }
    } else if (menu.type === 2 && menu.url === currentPath) {
      return menu
    }
  }
}

使用呢??

  <el-aside :width="isCollapse ? '60px' : '210px'">
    <nav-menu :collapse="isCollapse" />
  </el-aside>

如果不明白的可以去github里面有所有代码,github项目地址https://github.com/lsh555/vue3-ems

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3和Element Plus提供了一种简单的方法来实现侧边菜单栏与标签页之间的联动。下面是一个基本示例,演示了如何实现这一功能: 1. 首先,确保你已经安装了Vue 3和Element Plus,并在项目中引入它们。 2. 创建一个侧边菜单栏组件(SideMenu),用于展示菜单项。这个组件可以使用Element Plus的Menu组件来创建。 ```html <template> <el-menu :default-active="activeMenu" @select="handleMenuSelect"> <el-menu-item v-for="item in menuItems" :key="item.path" :index="item.path"> {{ item.name }} </el-menu-item> </el-menu> </template> <script> export default { data() { return { activeMenu: '', // 当前选中的菜单项 menuItems: [ { name: '菜单1', path: '/menu1' }, { name: '菜单2', path: '/menu2' }, // 其他菜单项... ] } }, methods: { handleMenuSelect(index) { this.activeMenu = index; // 更新选中的菜单项 this.$router.push(index); // 导航到对应的路由 } } } </script> ``` 3. 创建一个标签页组件(Tabs),用于展示已打开的页面。这个组件可以使用Element Plus的Tabs组件来创建。 ```html <template> <el-tabs v-model="activeTab" type="border-card" @tab-remove="handleTabRemove"> <el-tab-pane v-for="tab in openedTabs" :key="tab.path" :label="tab.name" :name="tab.path"> <!-- 页面内容 --> </el-tab-pane> </el-tabs> </template> <script> export default { data() { return { activeTab: '', // 当前选中的标签页 openedTabs: [] // 已打开的标签页 } }, methods: { addTab(tab) { const index = this.openedTabs.findIndex(item => item.path === tab.path); if (index === -1) { this.openedTabs.push(tab); } this.activeTab = tab.path; // 选中新打开的标签页 }, removeTab(targetName) { const index = this.openedTabs.findIndex(item => item.path === targetName); if (index !== -1) { this.openedTabs.splice(index, 1); } }, handleTabRemove(targetName) { this.removeTab(targetName); if (targetName === this.activeTab) { this.activeTab = this.openedTabs[this.openedTabs.length - 1].path; } } } } </script> ``` 4. 在你的主页面中,使用以上两个组件来实现联动效果。 ```html <template> <div> <side-menu></side-menu> <tabs></tabs> </div> </template> <script> import SideMenu from './SideMenu.vue'; import Tabs from './Tabs.vue'; export default { components: { SideMenu, Tabs } } </script> ``` 在这个例子中,当你点击侧边菜单栏的菜单项时,会更新选中的菜单项,并导航到对应的路由。同时,会在标签页中打开一个新的标签页。如果你点击已打开的标签页的关闭按钮,会关闭该标签页并自动选中最后一个打开的标签页。 这样就实现了侧边菜单栏与标签页的联动效果。你可以根据自己的需求进行定制和扩展。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值