elementui递归组件生成无限菜单

Aside.vue:

<template>
  <section class="aside">
    <el-menu
      text-color="#686a6e"
      active-text-color="#1a8aff"
      router
      :default-active="$route.path"
      :collapse="isCollapse"
    >
      <MenuList v-model:menuList="menuList" />
    </el-menu>
  </section>
</template>

<script lang="ts">
import { defineAsyncComponent, defineComponent, ref } from 'vue'
const useInitData = () => {
  const menuList = [
    {
      id: 1,
      title: '数据预览',
      icon: 'el-icon-pie-chart',
      path: '/layout/chart',
    },
    {
      id: 2,
      title: '用户列表',
      icon: 'el-icon-user',
      path: '/layout/user',
    },
    {
      id: 3,
      title: '题库列表',
      icon: 'el-icon-edit-outline',
      path: '/layout/question',
    },
    {
      id: 4,
      title: '企业列表',
      icon: 'el-icon-office-building',
      path: '/layout/business',
    },
    {
      id: 5,
      title: '学科列表',
      icon: 'el-icon-notebook-2',
      path: '/layout/subject',
    },
  ]
  return {
    menuList,
  }
}
export default defineComponent({
  name: 'Aside',
  props: ['isCollapse'],
  setup() {
    const initData = useInitData()
    return {
      ...initData,
    }
  },
  components: {
    MenuList: defineAsyncComponent({
      loader: () => import('@/pages/layout/components/layout/components/MenuList.vue'),
    }),
  },
})
</script>

<style lang="scss" scoped>
.aside {
  .el-menu {
    // 展开时的动画
    transition: width 0.2s;
    border-right: none;
    min-width: 200px;
    .el-menu-item {
      &:focus {
        background-color: #fff;
      }
    }
  }
}
</style>

 菜单组件menulist.vue:

<template>
  <section class="menuList">
    <template v-for="item in menuList" :key="item.id">
      <el-submenu :index="item.path" v-if="item.children">
        <template #title>
          <i :class="[item.icon]" />
          <span>{{ item.title }}</span>
        </template>
        <MenuList v-model:menuList="item.children" />
      </el-submenu>
      <el-menu-item v-else :index="item.path">
        <template #title>
          <i :class="[item.icon]" />
          <span>{{ item.title }}</span>
        </template>
      </el-menu-item>
    </template>
  </section>
</template>

<script>
/**
 * 递归生成菜单组件
 */
import { defineAsyncComponent } from 'vue'
export default {
  name: 'MenuList',
  props: ['menuList'],
  components: {
    MenuList: defineAsyncComponent({
      loader: () => import('@/pages/layout/components/layout/components/MenuList.vue'),
    }),
  },
}
</script>

<style lang="scss" scoped>
.menuList {
}
</style>

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值