vue3+elementplus无限级菜单

1.在项目中安装elementplus

npm install element-plus --save

2.在项目入口文件中使用

import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'

const app = createApp(App)

app.use(ElementPlus)
app.mount('#app')

3.创建菜单使用组件MainView 及子菜单渲染组件MenuItem

// ========MainView中的代码
<template>
  <div class="box">
      <el-menu
        default-active="2"
        class="el-menu-vertical-demo"
        :collapse="isCollapse"
        @open="handleOpen"
        @close="handleClose"
      >
<!-- menu-item为子组件(菜单渲染)并通过item把数据传递 给menu-item组件 -->
      <menu-item v-for="item in sliders" :key="item.indexPath" :item="item"  />

      </el-menu>
    </div>
</template>
<script setup>
import MenuItem from "@/components/MenuItem.vue";
// 得到菜单中的数据
const sliders = ref([        
        {
            "indexPath": "2",
            "title": "文章管理",
            "icon": "List",
            "children": [
                {
                    "indexPath": "/art-cate",
                    "title": "文章分类",
                    "icon": "Histogram"
                },
                {
                    "indexPath": "/art-list",
                    "title": "文章列表",
                    "icon": "DocumentCopy"
                }
            ]
        },
        {
            "indexPath": "3",
            "title": "个人中心",
            "icon": "User",
            "children": [
                {
                    "indexPath": "/user-info",
                    "title": "基本资料",
                    "icon": "Operation"
                },
                {
                    "indexPath": "/user-avatar",
                    "title": "更换头像",
                    "icon": "Camera"
                },
                {
                    "indexPath": "/user-pwd",
                    "title": "重置密码",
                    "icon": "Key"
                }
            ]
        }
    ])
// 请求

</script>

MenuItem.vue组件中的代码如下:

<template>
    <el-menu-item v-if="!item.children" :index="item.indexPath">
      <el-icon><component class="icon" :is="item.icon"></component></el-icon>
      <span :data-path="item.indexPath">{{ item.title }}</span>
    </el-menu-item>
   
    <el-sub-menu v-else :index="item.indexPath">
      <template #title>
        <el-icon><component class="icon" :is="item.icon"></component></el-icon>
        <span>{{ item.title }}</span>
      </template>
   
      <!-- 组件自调 -->
      <s-menu
        v-for="ele in item.children"
        :index="ele.indexPath"
        :key="ele"
        :item="ele"
      />
    </el-sub-menu>
  </template>
  <script setup>
  defineOptions({
    name: "SMenu",//组件命名
  });
  defineProps(["item"]);//父组件向子组件传递的数据

  </script>

注意:在MenuItem.vue组件中<component class="icon" :is="item.icon"></component>,在elementplus中icon是一个图标组件,需要以下步骤:

1.安装icon图标

npm install @element-plus/icons-vue

2.在入口文件中导入图标

// 如果您正在使用CDN引入,请删除下面一行。
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

最后运行效果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值