vue-element-admin给侧边栏增加统计数字标签,点击一次更新一次,也可只渲染一次

发现大家都有疑问,特此奉上demo ,仅供学习,不承担任何责任!!!

发现大家都有疑问,特此奉上demo ,仅供学习,不承担任何责任!!!

发现大家都有疑问,特此奉上demo ,仅供学习,不承担任何责任!!!

如何运行请查看README-zh.md
如何运行请查看README-zh.md
如何运行请查看README-zh.md
————————————————
链接: https://pan.baidu.com/s/174CqVvgPgtjgTAVYXcunbw?pwd=gv5t 提取码: gv5t

————————————————

上效果

多级菜单没试过,我弄的是单页面的一级菜单。多级菜单应该可以参考类似
在这里插入图片描述

在这里插入图片描述

  1. 打开src\router\index.js路径 修改路由配置
 {
    path: '/continuous_attention',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'continuous_attention',
        component: () => import('路径'),
        meta: { title: '菜单名', icon: 'eye-open' ,AttC:0}
        //Attc为统计数量的值,设置占位符,如果不需要则不填该字段
        //meta: { title: '菜单名', icon: 'eye-open' }//无状态标签填法
      }
    ]
  },
  1. 打开src\layout\components\Sidebar\SidebarItem路径 第六行左右的item标签给组件传入AttC参数
 <item :icon="onlyOneChild.meta.icon||(item.meta&&item.meta.icon)" :title="onlyOneChild.meta.title" :AttC='onlyOneChild.meta.AttC' /> 
  1. 进入item文件src\layout\components\Sidebar\Item.vue 添加参数
<script>

export default {
  name: 'MenuItem',
  functional: true,
  props: {
    icon: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: ''
    },
    AttC: {//接收父类传入参数
      type: Number
    }
  },
  render(h, context) {
    const { icon, title,AttC } = context.props
    const vnodes = []
    var _this = this
  
    if (icon) {
      vnodes.push(<svg-icon icon-class={icon}/>)
    }
    if (title) {
        vnodes.push(<span slot='title'>{(title)} </span>)
    }
    if(AttC >=0){ //判断一下 自动添加到对应的菜单上
      vnodes.push(<span slot='title'><el-tag type="danger"    size='mini' effect="dark" style='margin-top:-2px'   >{(AttC)}</el-tag></span>)
    }
    
    return vnodes
  },
}
</script>

  1. 进入组件index页面 src\layout\components\Sidebar\index
    需求一:点击侧边菜单刷新一次数据。增加点击回调函数handleSelect方法
    需求二:只渲染一次。取消回调函数handleSelect方法,删除@select="handleSelect",只在created方法中调用一次即可
<template>
  <div :class="{'has-logo':showLogo}">
    <logo v-if="showLogo" :collapse="isCollapse" />
    <el-scrollbar wrap-class="scrollbar-wrapper">
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :background-color="variables.menuBg"
        :text-color="variables.menuText"
        :unique-opened="false"
        :active-text-color="variables.menuActiveText"
        :collapse-transition="false"
        mode="vertical"
        @select="handleSelect"//增加高亮回调函数,如果只想执行一次删除这句
      >
        <sidebar-item v-for="route in permission_routes" :key="route.path" :item="route" :base-path="route.path" />
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Logo from './Logo'
import SidebarItem from './SidebarItem'
import variables from '@/styles/variables.scss'

import { getAttC } from '@/api/components'//从后台获取对应统计参数,自己去封装axios吧

export default {
  components: { SidebarItem, Logo },
  computed: {
    ...mapGetters([
      'permission_routes',
      'sidebar'
    ]),
    activeMenu() {
      const route = this.$route
      const { meta, path } = route
      // if set path, the sidebar will highlight the path you set
      if (meta.activeMenu) {
        return meta.activeMenu
      }
      return path
    },
    showLogo() {
      return this.$store.state.settings.sidebarLogo
    },
    variables() {
      return variables
    },
    isCollapse() {
      return !this.sidebar.opened
    }
  },  
  created(){
    this.handleSelect()
  },
  methods:{
    handleSelect(e){
        var _this = this
        getAttC({}).then(item => { 
           _this.permission_routes.forEach(element => { //获取当前所有参数 找到需要
             if(element.path == '/continuous_attention'){ //判断列表路由path,对应第一点中path字段
               element.children[0].meta.AttC = item.data //改变渲染初值
               return
             }
          }); 
        }) 
    }
  }
}
</script>

  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 29
    评论
Vue-element-admin 是一个基于 Vue.js 和 Element 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 组件来展示菜单项。侧边栏组件的主要作用是传递菜单项列表、当前选中的菜单项和侧边栏是否折叠等参数,以及监听菜单项的点击事件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小龍

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

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

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

打赏作者

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

抵扣说明:

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

余额充值