SoFunction
Updated on 2025-04-05

How to dynamically generate lower-level menus in the vue sidebar

Loop the incoming data to generate the following menu

<template>
 <div class="headBar">
 <div class="title">
  Micro Mall Management Backend
 </div>
 <el-menu
  class="el-menu-headBar"
  mode="horizontal"
  @select="handleSelect"
  background-color="#000000"
  text-color="#fff"
  active-text-color="#ffd04b"
  :unique-opened="true"
  :default-active="onRoutes" router>
  <template v-for="item in items" >
  <template v-if="" >
   <el-submenu :index="">
   <template slot="title" >
    {{}}
   </template>
   <el-menu-item v-for="(subItem,i) in " :key="i" :index="">
    {{  }}
   </el-menu-item>
   </el-submenu>
  </template>
  <template v-else>
   <el-menu-item :index="">
   <label>{{  }}</label>
   </el-menu-item>
  </template>
  </template>
 </el-menu>
 </div>
</template>
 
<script>
 export default {
 data() {
  return {
  activeIndex: "1",
  items: [
   {
   icon: 'el-icon-menu',
   index: '1',
   title: 'Data statistics',
   subs: [
    {
    index: '/monitor',
    title: 'Catchment flow display'
    },
    {
    index: '/monitor/device',
    title: 'Equipment Collection'
    },
    {
    index: '/monitor/tv',
    title: 'Surveillance video'
    }
   ]
   },{
   icon: 'el-icon-goods',
   index: '/product',
   title: 'Commodity Management',
   },{
   icon: 'el-icon-goods',
   index: '/category',
   title: 'Category Management',
   },{
   icon: 'fa fa-cart-arrow-down',
   index: '/order',
   title: 'Order List'
   },{
   icon: 'fa fa-user-o',
   index: '/merchant',
   title: 'Business Information'
   },{
   icon: 'el-icon-printer',
   index: '9',
   title: 'WeChat',
   subs:[
    {
    icon: 'el-icon-printer',
    index: '/banner',
    title: 'banner settings'
    },{
    icon: 'el-icon-printer',
    index: '/decoration',
    title: 'Home page decoration'
    },{
    icon: 'el-icon-printer',
    index: '/message',
    title: 'Message Settings'
    }
   ]
   },{
   icon: 'el-icon-printer',
   index: '10',
   title: 'Offline store',
   subs:[
    {
    icon: 'el-icon-printer',
    index: '/device',
    title: 'Equipment Management'
    },{
    icon: 'el-icon-printer',
    index: '/advertise',
    title: 'Advertising Management'
    },{
    icon: 'el-icon-printer',
    index: '/version',
    title: 'version management'
    }
   ]
   },{
   icon: 'el-icon-printer',
   index: '/largeUI',
   title: 'Big Screen'
   },{
   icon: 'el-icon-printer',
   index: '/coupon',
   title: 'Coupon'
   }
  ],
  }
 },
 methods: {
  handleSelect(key, keyPath) {
  (key, keyPath);
  }
 },
 computed:{
  onRoutes(){
  return this.$('/','');
  }
 }
 }
</script>
 
<style scoped>
 .el-menu-headBar {
 width: 80%;
 min-width: 950px;
 font-size: 12px;
 border-bottom: 1px #000000;
 }
 
 .headBar {
 width: 100%;
 height: 50px;
 display: flex;
 background-color: #000000;
 }
 
 .title {
 background-color: #ffdb15;
 color: #000;
 height: 100%;
 min-width: 200px;
 width: 200px;
 display: flex;
 justify-content: center;
 align-items: center;
 letter-spacing: 5px;
 font-size: 17px;
 }
</style>

The above method of dynamically generating the lower menu in the vue sidebar is all the content I share with you. I hope you can give you a reference and I hope you can support me more.