vue3+element-plus的侧边菜单栏的图标的动态渲染

本文介绍了如何在ElementPlus框架中使用Vue全局注册NavMenu导航菜单的图标,包括安装ElementPlusIconsVue,以及在组件模板中通过数组动态显示不同图标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Element-Ui组件 NavMenu 导航菜单图标的具体使用

在这里插入图片描述

首先要安装依赖

npm install @element-plus/icons-vue

//如果你已经全局安装element-plus的话就不需要安装这个了,直接按照下面的步骤走就行了

在main.js中全局导入

import * as ElementPlusIconsVue from '@element-plus/icons-vue'

通过循环注册icon

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

在组件内部使用,首先定义图标的数组

// 菜单图标
const icons = reactive([
  'User',
  'SwitchFilled',
  'Document',
  'MessageBox',
  'Location',
  'Shop',
  'Orange',
  'Suitcase',
  'VideoPlay'
])

在模板内进行使用

    <el-sub-menu index="1" v-for="(item, index) in menuList" :keys="item.id">
              <template #title>
                <el-icon>
                  <component :is="icons[index]"></component>
                </el-icon>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item :index="itemChild.id" v-for="(itemChild, indexChild) in item.children" :keys="itemChild.id">
                <el-icon>
                  <Location />
                </el-icon>
                <span> {{ itemChild.authName }}</span>
              </el-menu-item>


            </el-sub-menu>
<el-icon>
                  <component :is="icons[index]"></component>
                </el-icon>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值