vue3 element-plus动态菜单及动态图标

这篇具有很好参考价值的文章主要介绍了vue3 element-plus动态菜单及动态图标。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1. 安装element-plus及图标库

# NPM
$ npm install @element-plus/icons-vue
# Yarn
$ yarn add @element-plus/icons-vue
# pnpm
$ pnpm install @element-plus/icons-vue


# NPM
$ npm install element-plus --save

# Yarn
$ yarn add element-plus

# pnpm
$ pnpm install element-plus

2.全局引入

引入element-plus

//引入element-plus
import ElementPlus from 'element-plus'
const app = createApp(App)
app.use(ElementPlus)

注册图标组件

// main.ts

// 如果您正在使用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)
}

3. 动态菜单代码

动态引入图标代码

<el-icon>
   <component  v-if="item.meta" :is="item.meta.icon"></component>
</el-icon>

完整代码

<template>
  <el-menu
    active-text-color="#ffd04b"
    background-color="#545c64"
    text-color="#fff"
    default-active="2"
    router
    class="el-menu-vertical-demo"
    :collapse="props.isCollapse"
    :ellipsis="false"
    @open="handleOpen"
    @close="handleClose"
  >
    <!-- <el-sub-menu index="/home">
      <template #title>
        <el-icon><location /></el-icon>
        <span>常用功能</span>
      </template>
        <el-menu-item index="/throttle">节流</el-menu-item>
        <el-menu-item index="/debounce">防抖</el-menu-item>
    </el-sub-menu>
    <el-menu-item index="2">
      <el-icon><icon-menu /></el-icon>
      <template #title>Navigator Two</template>
    </el-menu-item>
    <el-menu-item index="3" disabled>
      <el-icon><document /></el-icon>
      <template #title>Navigator Three</template>
    </el-menu-item>
    <el-menu-item index="4">
      <el-icon><setting /></el-icon>
      <template #title>Navigator Four</template>
    </el-menu-item> -->
    <template v-for="item in menuRoutes" :key="item.id">
        <el-sub-menu  v-if="item.children && item.children.length > 0" :index="item.path">
          <template #title>
            <el-icon>
              <component  v-if="item.meta" :is="item.meta.icon"></component>
            </el-icon>
              <!-- <el-icon><location /></el-icon> -->
              <span v-if="item.meta">{{item.meta.title}}</span>
          </template>
          <!-- 二级菜单 -->
            <el-menu-item v-for="item2 in item.children" :key="item2.id" :index="item2.path" :style="item2.children?'display: none': null">
              {{item2.children? null : item2.meta.title}}
            </el-menu-item>
          <!-- 三级菜单 -->
            <el-sub-menu v-for="item2 in item.children.filter(x => x.children && x.children.length > 0)" :key="item2.id" class="child-sub-menu" :index="item2.path">
              <template #title>
                <!-- <component  v-if="item.meta" :is="item.meta.icon" style="width: 20px; height: 20px"></component> -->
                <!-- <el-icon><location /></el-icon> -->
                  <span v-if="item2.meta">{{item2.meta.title}}</span>
              </template>
              <el-menu-item v-for="item3 in item2.children" :key="item3.id" :index="item3.path">{{item3.meta.title}}</el-menu-item>
            </el-sub-menu>
          <!-- </template> -->
        </el-sub-menu>
        <el-menu-item v-else :index="item.path">{{item.meta.title}}</el-menu-item>
    </template>
  </el-menu>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { menuRoutes } from '@/router/index'
const router = useRouter()
const props = defineProps({
  isCollapse: {
		type: Boolean,
		default: false,
  }
})
const emits = defineEmits(['menuChange'])
const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
onMounted(() => {
    console.log(router.options.routes)
})
</script>

<style>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>
<style lang="stylus" scoped>
.el-menu
  height: 100vh
  border-right: none
:deep(.el-sub-menu__title)
  height: 50px !important
</style>

路由如下文章来源地址https://www.toymoban.com/news/detail-802734.html

export const menuRoutes: Array<RouteRecordRaw> = [
    {
        path: '/adminHome',
        name: 'adminHome',
        meta: {
            title: '常用功能',
            icon: 'location'
        },
        component: () => import('@/views/adminHome.vue'),
        children: [
            {
                path: '/debounce',
                name: 'debounce',
                meta: {
                    title: '防抖',
                    icon: 'location'
                },
                component: () => import('@/views/adminsystem/debounce/index.vue')
            },
            {
                path: '/throttle',
                name: 'throttle',
                meta: {
                    title: '节流',
                    icon: 'location'
                },
                component: () => import('@/views/adminsystem/throttle/index.vue')
            }
        ]
    }
]

到了这里,关于vue3 element-plus动态菜单及动态图标的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue3.x结合element-plus如何使用icon图标

     基于 Vue 3的Element Plus如何使用icon图标 首先注意Element Plus版本:官网如图所示,  基于vue3的具体如何使用: 参考官网文档: 1.首先选择一种方式安装  2.然后全局注册图标 在main.js或main.ts文件中引入:  3.然后就可以使用了,具体实例如下: 使用方式1:输入框中使用 输入框

    2023年04月08日
    浏览(38)
  • Vue3 element-plus表单嵌套表格实现动态表单验证

    部分效果图如下: 另表格有添加和删除按钮,点击提交进行表单验证。 首先data格式必须是对象包裹数组 给表单绑定form数据 表格绑定tableData数据 给表单项增加验证规则 rules对应data rules对象,prop对应表单字段(注意是表格里每一行对应的字段 forms.tableData[下标].key) prop的关

    2024年02月14日
    浏览(32)
  • Vue3中动态绑定:disabled element-plus使用方法

    @change=\\\"whetherFlag($event)\\\"  根据value值判断是否禁用 :disabled=\\\"isShow\\\" 初始值为禁用状态 const isShow = refboolean(true);  根据value的值判断是否禁用  

    2024年01月25日
    浏览(41)
  • element-plus 动态Icon图标

    目录 1,前言 2,使用 2.1,方式一 2.2,方式二 源自 Element Plus 团队正在将原有组件内的 Font Icon 向 SVG Icon 迁移,请多多留意更新日志, 及时获取到更新信息,Font Icon 将会在第一个正式发布被废弃,请尽快迁移 在此记录一下如何使用element-plus中的icon组件 环境: Vue:3.2.16 Elem

    2024年02月16日
    浏览(29)
  • Vue3 + Element Plus 实现动态标签页及右键菜单

    目录 先上图  使用el-dropdown绑定右键菜单,为每个tab页绑定一个右键 右键菜单生效后控制每个下拉项的禁用与显示(每一项代表一个功能) 每个右键项对应的功能  控制每次只显示一个右键 完整代码         只有首页的情况         多个tab页的情况  

    2024年02月07日
    浏览(31)
  • Vue3+Element-Plus 实现用户列表页面的UI结构及动态加载表单功能 三一

    1.1 头部是一个面包屑 (Breadcrumb)导航区域 1.2 白色区域是一个卡片(Card)视图 1.3 卡片 (Card)视图中嵌套了   输入框(Input )、 按钮(Button)、 表单(Form)、分页(Pagination ) Breadcrumb 面包屑 | Element Plus (gitee.io) https://element-plus.gitee.io/zh-CN/component/breadcrumb.html  2.1.1 复制

    2023年04月09日
    浏览(44)
  • vue3+ts+element-plus 之使用node.js对接mysql进行表格数据展示

    * 初始化node 查看node是否安装 node -v 初始化命令 npm init 初始化配置解释如下: 完成后会有一个package.json文件 * 安装可能用到的依赖 根据需求安装,我这里需要对接mysql,安装依赖 ,我是一次性安装完,后边会直接使用,也可以边安装边使用。如下 安装成功如下: * 配置文件

    2024年02月15日
    浏览(33)
  • Vue3+Vue-Router+Element-Plus根据后端数据实现前端动态路由——权限管理模块

    提示:文章内容仔细看一些,或者直接粘贴复制,效果满满 提示:文章大概 1、项目:前后端分离 2、前端:基于Vite创建的Vue3项目 3、后端:没有,模拟的后端数据 4、关于路径“@”符号——vite.config.js 文件里修改 提示:以下是本篇文章正文内容,下面案例可供复制粘贴使用

    2024年02月02日
    浏览(38)
  • vue3使用element-plus

    element-ui 是配合 vue2 使用,element-plus 是配置 vue3 使用的 1. 包管理器的方式 如果是使用 webpack 或者 vite 打包工具新建的项目 2. 浏览器直接导入 直接通过浏览器的 HTML 标签导入 Element Plus,然后就可以使用全局变量 ElementPlus 1. 导入全部组件且注册所有的图标 声明使用 ElementPl

    2024年02月08日
    浏览(56)
  • Vue3导入Element-plus方法

    先引入依赖 main.js中要引入两个依赖 然后 这个东西 我们最好还是挂载vue上 所以 还是 然后 我们可以在组件上试一下用一个ElementUi的表格组件 参考代码如下 运行结果如下 也是没有任何问题

    2024年02月06日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包