el-menu使用遞迴元件實現多級選單元件

Li_pk發表於2023-04-12

1. 效果:

image
image image

2. 實現:

建立外層選單AsideMenu.vue元件和子選單項AsideSubMenu.vue元件,在AsideSubMenu中進行遞迴操作。

AsideMenu.vue檔案內容如下:

<template>
  <aside class="wrap">
    <el-menu
      :default-active="activeMenu"
      router
      :class="'menu-left'"
      :default-openeds="openedsArr"
      text-color="#fff"
    >
      <AsideSubMenu :menuData="menuData"></AsideSubMenu>
    </el-menu>
  </aside>
</template>

<script>
import AsideSubMenu from "./AsideSubMenu.vue";
export default {
  name: "AsideMenu",
  components: {
    AsideSubMenu,
  },
  props: {
    menuData: {
      type: Array,
    },
  },
  computed: {
    activeMenu() {
      const route = this.$route;
      const { meta, path } = route;
      // 此處新增判斷的原因見說明
      if (meta.matchPath) {
        return meta.matchPath;
      } else {
        return path;
      }
    },
    // 設定預設展開選單項
    openedsArr() {
      // const arr = this.menuData.map((item) => {
      //   return item.title;
      // });
      // return arr;
      return [];
    },
  },
};
</script>

判斷高亮狀態的activeMenu方法中的判斷matchPath屬性可以讓多個路由不同的頁面匹配同一個選單高亮狀態,因為選單高亮狀態是根據路由地址匹配的。如果兩個不同的路由頁面想公用同一個選單高亮狀態(如詳情頁面和列表頁)就可以使用該方法實現。在router檔案裡設定meta物件,新增matchPath屬性設定為想要共用的高亮狀態的頁面的路由地址。(有點繞?)

樣式根據需求修改,示例中的樣式如下(此處包含深淺兩種主題的選單樣式):

點選檢視程式碼
<style scoped>
aside {
  height: 100%;
  text-align: center;
}
.el-menu {
  padding: 16px;
  box-sizing: border-box;
}
/* ---------- 深色 ---------- */
.menu-left,
.menu-left /deep/ .el-menu {
  min-height: 100%;
  background-color: #222653;
}
.menu-left .icon {
  width: 20px;
  margin-right: 9px;
}
.menu-left /deep/.el-submenu__title,
.menu-left /deep/.el-menu-item {
  box-sizing: border-box;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  text-align: left;
  color: #b3c0e7 !important;
  background-color: #222653 !important;
}
.menu-left /deep/.el-submenu__title i {
  color: #b3c0e7 !important;
}
.menu-left /deep/.el-submenu__title {
  height: 54px;
  line-height: 54px;
  /* padding-left: 36px !important; */
}
.menu-left /deep/ .el-menu-item {
  height: 52px;
  line-height: 52px;
}
.menu-left /deep/.el-submenu .el-menu-item {
  padding-left: 45px !important;
}
/* 外層高亮 */
.menu-left /deep/.el-submenu.is-active,
.menu-left /deep/.el-submenu.is-active .el-menu-item,
.menu-left /deep/.el-submenu.is-active .el-submenu__title,
.menu-left /deep/.el-menu-item.is-active {
  background-color: #4880ff !important;
}
.menu-left /deep/.el-submenu.is-active {
  border-radius: 10px;
  overflow: hidden;
}
.menu-left /deep/ .el-menu-item.is-active {
  border-radius: 10px;
}
.menu-left /deep/ .el-menu--inline .el-menu-item.is-active,
.menu-left /deep/ .el-submenu.noIcon {
  border-radius: 0;
}
.menu-left /deep/ .el-submenu.noIcon .el-submenu__title {
  padding-left: 45px !important;
}
.menu-left /deep/ .el-submenu.noIcon .el-menu-item {
  padding-left: 58px !important;
}
.menu-left /deep/.el-submenu.is-active > .el-submenu__title,
.menu-left /deep/.el-submenu.is-active > .el-submenu__title i {
  color: #ffffff !important;
}
/* 內層高亮 */
.menu-left /deep/.el-menu-item:focus,
.menu-left /deep/.el-menu-item:hover,
.menu-left /deep/.el-menu-item.is-active {
  color: #ffffff !important;
  font-weight: 500;
}
.menu-left /deep/.el-menu-item.is-disabled {
  padding-left: 45px !important;
  color: #ffffff !important;
}

/* ---------- 淺色 ---------- */
.menu-left-light {
  height: 100%;
  background-color: #f8f8f8;
}
.menu-left-light .icon {
  width: 20px;
  margin-right: 9px;
}
.menu-left-light /deep/.el-submenu__title,
.menu-left-light /deep/.el-menu-item {
  box-sizing: border-box;
  font-size: 16px;
  font-family: PingFangSC-Regular, PingFang SC;
  text-align: left;
  color: #333333;
}
.menu-left-light /deep/.el-submenu__title {
  height: 52px;
  line-height: 52px;
  padding-left: 56px !important;
}
.menu-left-light /deep/.el-submenu__title:hover {
  background-color: #ffffff !important;
}
.menu-left-light /deep/.el-submenu__icon-arrow {
  right: 85px;
}
.menu-left-light /deep/.el-submenu__title i {
  color: #333333;
}
.menu-left-light /deep/.el-menu-item {
  height: 40px;
  line-height: 40px;
  padding-left: 82px !important;
  border-left: 4px solid #ffffff;
}
.menu-left-light /deep/.el-menu-item:focus,
.menu-left-light /deep/.el-menu-item:hover,
.menu-left-light /deep/.el-menu-item.is-active {
  background: rgba(31, 65, 219, 0.1) !important;
  color: #1f41db !important;
  border-color: #1f41db;
}
.menu-left-light /deep/.el-menu-item.is-disabled {
  background: #ffffff !important;
  color: #333333 !important;
}
</style>

AsideMenu.vue檔案內容如下:

<template>
  <div>
    <template v-for="item in menuData">
      <el-submenu
        :key="item.path"
        v-if="item.children && item.children.length > 0"
        :index="item.path"
        :class="item.icon ? '' : 'noIcon'"
      >
        <template slot="title">
          <img
            class="icon mr-r-10"
            :src="
              curRoute.indexOf(item.path) != -1 ? item.iconActive : item.icon
            "
          />
          <span>{{ item.title }}</span>
        </template>
        <AsideSubMenu :menuData="item.children"></AsideSubMenu>
      </el-submenu>
      <el-menu-item
        :key="item.id"
        v-else
        :index="item.path"
        :disabled="item.disabled"
      >
        <template slot="title">
          <img
            class="icon mr-r-10"
            :src="
              curRoute.indexOf(item.path) != -1 ? item.iconActive : item.icon
            "
          />
          <span>{{ item.title }}</span>
        </template>
      </el-menu-item>
    </template>
  </div>
</template>

判斷如果有子選單則進行遍歷操作。同時此處根據是否有icon給el-submenu動態新增了一個類名:class="item.icon ? '' : 'noIcon'",這麼做是由於高亮狀態下的.el-submenu新增了圓角效果,在存在多層子選單巢狀的情況下如果不清除圓角效果則會出現問題(見下圖)。這個狀態下不好用選擇器選中需要操作的元素,因此根據是否有icon這個區別進行了區分。如果是整個選單都沒有icon的情況的話,那暫時還沒想好應對策略。?

image

<script>
import AsideSubMenu from "./AsideSubMenu.vue";
export default {
  name: "AsideSubMenu",
  components: {
    AsideSubMenu,
  },
  props: {
    menuData: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  computed: {
    curRoute() {
      return this.$route.path;
    },
  },
};
</script>

3. 使用元件:

  • 新增路由配置;
  • 引入並掛載元件;
  • 傳入選單資料;

程式碼如下:

<template>
  <el-container class="container">
    <el-aside width="320px">
      <AsideMenu :menuData="menuData"></AsideMenu>
    </el-aside>
    <el-main>
      <keep-alive :exclude="[]">
        <router-view></router-view>
      </keep-alive>
    </el-main>
  </el-container>
</template>

<script>
import AsideMenu from '@/components/AsideMenu.vue';
export default {
  name: 'MenuTest',
  components: {
    AsideMenu
  },
  data() {
    return {
      menuData: [
        {
          title: '選單一',
          path: '/menutest/menu1',
          icon: require('@/assets/icons/apply.svg'),
          iconActive: require('@/assets/icons/apply_active.svg'),
          children: [
            {
              title: '子選單一',
              path: '/menutest/menu1/menu1-1',
              // disabled: true,
            },
            {
              title: '子選單二',
              path: '/menutest/menu1/menu1-2'
            }
          ]
        },
        {
          title: '選單二',
          path: '/menutest/menu2',
          icon: require('@/assets/icons/apply.svg'),
          iconActive: require('@/assets/icons/apply_active.svg'),
          children: [
            {
              title: '子選單一',
              path: '/menutest/menu2/menu2-1'
            },
            {
              title: '子選單二',
              path: '/menutest/menu2/menu2-2',
              children: [
                {
                  title: '孫子選單一',
                  path: '/menutest/menu2/menu2-2/menu2-1-1'
                },
                {
                  title: '孫子選單二',
                  path: '/menutest/menu2/menu2-2/menu2-2-2'
                }
              ]
            },
            {
              title: '子選單三',
              path: '/menutest/menu2/menu2-3'
            }
          ]
        },
        {
          title: '選單三',
          path: '/menutest/menu3',
          icon: require('@/assets/icons/apply.svg'),
          iconActive: require('@/assets/icons/apply_active.svg'),
        }
      ]
    };
  }
};
</script>

<style scoped>
.container {
  min-height: 800px;
}
.el-main {
  padding: 32px 40px;
  box-sizing: border-box;
  background: #f5f5fa;
  overflow-y: auto;
}
</style>

示例中的路由配置如下:

  {
    path: "/menutest",
    name: "Menu",
    component: () => import("../views/MenuTest.vue"),
    redirect: "/menutest/menu1",
    children: [{
        path: '/menutest/menu1',
        component: () => import("../views/menuPages/menu1.vue"),
        children: [{
            path: '/menutest/menu1/menu1-1',
            component: () => import("../views/menuPages/menu1-1.vue"),
          },
          {
            path: '/menutest/menu1/menu1-2',
            component: () => import("../views/menuPages/menu1-2.vue"),
          }
        ]
      },
      {
        path: '/menutest/menu2',
        component: () => import("../views/menuPages/menu2.vue"),
        children: [{
            path: '/menutest/menu2/menu2-1',
            component: () => import("../views/menuPages/menu2-1.vue"),
          },
          {
            path: '/menutest/menu2/menu2-2',
            component: () => import("../views/menuPages/menu2-2.vue"),
            children: [{
                path: '/menutest/menu2/menu2-2/menu2-1-1',
                component: () => import("../views/menuPages/menu2-1-1.vue"),
              },
              {
                path: '/menutest/menu2/menu2-2/menu2-2-2',
                component: () => import("../views/menuPages/menu2-1-2.vue"),
              }
            ]
          },
          {
            path: '/menutest/menu2/menu2-3',
            component: () => import("../views/menuPages/menu2-3.vue"),
          }
        ]
      },
      {
        path: '/menutest/menu3',
        component: () => import("../views/menuPages/menu3.vue"),
      }
    ]
  }

相關文章