vue2 + element三级菜单实现模板

需求:

需要一个含有三级菜单的结构模板,用于业务快速开发。

解决:

sidebar.vue

复制代码
<template>
  <el-menu :default-active="defaultActive" class="el-menu-vertical-demo" 
    active-text-color="#ffd04b">
    <template v-for="menu in menus">
      <el-submenu :index="menu.index" v-if="menu.children && menu.children.length > 0">
        <template slot="title">
          <i class="el-icon-menu"></i>
          <span>{{ menu.name }}</span>
        </template>
        <template v-for="child in menu.children">
          <el-submenu :index="child.index" v-if="child.children && child.children.length > 0">
            <template slot="title">{{ child.name }}</template>
            <el-menu-item v-for="subChild in child.children" :index="subChild.index" :key="subChild.index">{{
    subChild.name
  }}</el-menu-item>
          </el-submenu>
          <el-menu-item v-else :index="child.index">{{ child.name }}</el-menu-item>
        </template>
      </el-submenu>
      <el-menu-item v-else :index="menu.index">{{ menu.name }}</el-menu-item>
    </template>
  </el-menu>
</template>

<script>
export default {
  data() {
    return {
      defaultActive: '1',
      menus: [
        {
          index: '1',
          name: '一级菜单1',
          children: [
            {
              index: '1-1',
              name: '二级菜单1-1',
              children: [
                { index: '1-1-1', name: '三级菜单1-1-1' },
                { index: '1-1-2', name: '三级菜单1-1-2' }
              ]
            },
            {
              index: '1-2',
              name: '二级菜单1-2',
              children: [
                { index: '1-2-1', name: '三级菜单1-2-1' },
                { index: '1-2-2', name: '三级菜单1-2-2' }
              ]
            }
          ]
        },
        {
          index: '2',
          name: '一级菜单2',
          children: [
            {
              index: '2-1',
              name: '二级菜单2-1',
              children: [
                { index: '2-1-1', name: '三级菜单2-1-1' },
                { index: '2-1-2', name: '三级菜单2-1-2' }
              ]
            }
          ]
        }
      ]
    };
  }
};
</script>

<style lang="less" scoped>
.el-menu {
  width: 287px;

  .el-menu-item {
    font-size: 16px;
    padding-left: 49px;

    &.is-active {
      //color: #fff;
      //background-color: #6c7c97;
      background-color: #fafafa;
      color: #409eff;
    }
  }
}

.el-submenu /deep/ .el-submenu__title {
  font-size: 16px;
}

.cuscRouter {
  // background-color: #fafafa;
  color: #409eff !important;
}
</style>
相关推荐
kyriewen1 天前
写组件文档写到吐?我用AI自动生成Storybook,同事以后直接抄
前端·javascript·面试
五点六六六1 天前
你敢信这是非Native页面写出来的渐变效果吗🌝(底层原理解析
前端·javascript·面试
吃西瓜的年年1 天前
TypeScript
javascript·ubuntu·typescript
独泪了无痕1 天前
CryptoJS:数据安全的JavaScript加密利器
前端·vue.js·node.js
熊猫_豆豆2 天前
一个模拟四轴飞行器在随机气流扰动下悬停飞行的交互式3D仿真网页,包含飞行器建模与PID控制算法
javascript·3d·html·四轴无人机模拟飞行
来恩10032 天前
jQuery选择器
前端·javascript·jquery
前端繁华如梦2 天前
树上挂苹果还是挂玻璃球?Three.js 程序化果实的完整实现指南
前端·javascript
CDwenhuohuo2 天前
优惠券组件直接用 uview plus
前端·javascript·vue.js
川冰ICE2 天前
TypeScript装饰器与元编程实战
前端·javascript·typescript
AI砖家2 天前
Vue3组件传参大全,各种传参方式的对比
前端·javascript·vue.js