【Vue element-admin 如何给侧边栏添加 Badge 计数标记 】

一、需求功能

需求说明:给 element-admin 的侧边菜单栏或及子菜单栏添加计数标记 el-badge

  • 需求视图如下:
    在这里插入图片描述

二、实现思路

  • 结合 icon 图标渲染的思路,通过在layout 的 item.vue 中使用 vnodes.push 方法实现对 <el-badge /> 的渲染。
  • 在通过 Vuex 的状态管理将菜单栏需要的数据转递过来
  • 过滤不需要的菜单栏
  • layout目录结构
    在这里插入图片描述

三、 具体实现步骤

1.渲染 <el-badge /> :src\layout\components\Sidebar\Item.vue

<script>
export default {
  props: {
    icon: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: ''
    }
  },
  render(h, context) {
    const { icon, title } = context.props
    const vnodes = []

	// icon图标
    if (icon) {
    	```
    	过滤
    	```
        vnodes.push(<svg-icon icon-class={icon} />)
    }

	// 侧边菜单栏标题
	 if (title) {
      vnodes.push(<span slot='title'>{(title)}</span>)
    }
    
    return vnodes
  }
}
</script>

2.状态管理 Store 参数获取

  • Store – commit
    通过 element-admin 的 permission.js 权限文件实现参数的 commit (根据自己项目的具体菜单权限,灵活变通)
···
// 找到项目的 menu 权限、在逻辑中添加接口请求、并提交给store

// 我的申请
const { applyData} = await request({ url, data})
store.commit('SET_MYAPPLYTOTAL', applyData.length)
                  
// 我的审核
const { approveData} = await request({ url, data})
store.commit('SET_AUDIOTOTAL', approveData.length)

···
  • Store – mutations
    接收commit 参数,并给定义的 state 参数赋值
const state = {
  audioTotal: 0, // 审核
  myApplyTotal: 0// 申请
}

const mutations = {
  SET_AUDIOTOTAL(state, data) {
    state.audioTotal = data
  },
  SET_MYAPPLYTOTAL(state, data) {
    state.myApplyTotal = data
  }
}

3.在渲染文件item.vue 中使用 store 参数

<script>
import store from '@/store'
export default {
  name: 'MenuItem',
  functional: true,
  props: {
    icon: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: ''
    }
  },
  render(h, context) {
    const { icon, title } = context.props
    const vnodes = []

    if (title) {
      if (title === '我的审核') {
        vnodes.push(
          <span slot='title'>
            {title}
            <el-badge value={store.state.user.audioTotal} />
          </span>
        )
      }
      	```
    	其他菜单过滤同理
    	```
      else {
        vnodes.push(<span slot='title'>{title}</span>)
      }
    }
    return vnodes
  }
}
</script>




结语

初心:希望我的方案能给你一点思路
期盼:如果你有更好的解决方法,期盼你能分享到评论区

  • 11
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
Vue-element-admin 是一个基于 Vue.jsElement UI 的后台管理系统解决方案。它提供了许多常用的后台管理功能和组件,包括侧边栏、面包屑导航、表格、表单、图表等等。 侧边栏Vue-element-admin 中的一个重要组件,它通常用于展示系统的菜单和功能项。在 Vue-element-admin 中,侧边栏是通过路由配置来生成的。 在路由配置中,每一个路由配置项对应着一个菜单项。菜单项包括菜单的标题、图标、路径、子菜单等信息。当用户点击菜单项时,就会跳转到对应的路由页面。 以下是一个简单的示例,演示如何使用 Vue-element-admin侧边栏组件: ```javascript // 路由配置 const routes = [ { path: '/', redirect: '/dashboard' }, { path: '/dashboard', component: Layout, children: [ { path: '', name: 'Dashboard', component: () => import('@/views/dashboard/index'), meta: { title: 'Dashboard', icon: 'dashboard' } } ] }, { path: '/example', component: Layout, redirect: '/example/table', name: 'Example', meta: { title: 'Example', icon: 'example' }, children: [ { path: 'table', name: 'Table', component: () => import('@/views/table/index'), meta: { title: 'Table', icon: 'table' } }, { path: 'tree', name: 'Tree', component: () => import('@/views/tree/index'), meta: { title: 'Tree', icon: 'tree' } } ] } ] // 侧边栏组件 <template> <el-menu :default-active="activeIndex" class="el-menu-vertical-demo" :collapse="isCollapse"> <template v-for="(item, index) in menuList"> <el-submenu v-if="item.children" :key="index" :index="index"> <template slot="title"> <i :class="item.icon"></i> <span slot="title">{{item.title}}</span> </template> <template v-for="(subItem, subIndex) in item.children"> <el-menu-item :key="index + '-' + subIndex" :index="index + '-' + subIndex" @click="handleMenuClick(subItem)"> <i :class="subItem.icon"></i> <span slot="title">{{subItem.title}}</span> </el-menu-item> </template> </el-submenu> <el-menu-item v-else :key="index" :index="index" @click="handleMenuClick(item)"> <i :class="item.icon"></i> <span slot="title">{{item.title}}</span> </el-menu-item> </template> </el-menu> </template> <script> export default { props: { menuList: { type: Array, required: true }, activeIndex: { type: String, required: true }, isCollapse: { type: Boolean, required: true } }, methods: { handleMenuClick(item) { this.$emit('menu-click', item) } } } </script> ``` 在这个示例中,路由配置包含了三个路由项,分别对应 Dashboard、Table 和 Tree 三个菜单项。侧边栏组件则使用了 Element UI 中的 Menu 组件来展示菜单项。侧边栏组件的主要作用是传递菜单项列表、当前选中的菜单项和侧边栏是否折叠等参数,以及监听菜单项的点击事件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UiNMX_唐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值