Vue组件递归使用生成侧边栏

基本思路:根据路由信息生成侧边栏,由于路由信息是嵌套格式,会有多层嵌套,如果直接使用组件v-for指令来实现,需要多级的for循环嵌套,如果后台改变路由信息,则会更加麻烦。

组件是可以递归的!(没想到吧)

到底我该咋使用递归???

别慌别慌,下面代码就是一个实例:(一看就会,一写就...)

一个垃圾路由表:

leftMenu: [{
        "title": "工作台",
        "path": "/home",
        "icon": "el-icon-house",
        "children": [],
        id: 1
      }, {
        "title": "借贷管理",
        "path": "/debitManage",
        "icon": "el-icon-money",
        id: 1,
        "children": [{
            "title": "提交借款初审",
            "path": "/debitManage/firstCon",
            id: 1,
            "children": [{
              "title": "新增借款",
              "path": "/debitManage/addDebitItem",
              name: "addDebitItem",
              id: 1,
              "children": []
            }, {
              "title": "新标维护",
              "path": "/debitManage/upholdNewBidItems",
              name: "addDebitItem",
              id: 1,
              "children": []
            }, {
              "title": "借款审核",
              "path": "/debitManage/firstDebitCon",
              name: "addDebitItem",
              id: 1,
              "children": []
            }]
          }, {
            "title": "上架管理",
            id: 1,
            "path": "/debitManage/secondCon",
            "children": [{
              "title": "上架初审",
              "path": "/debitManage/firstLevelAuditItems",
              name: "addDebitItem",
              id: 1,
              "children": []
            }, {
              "title": "标的上架",
              "path": "/debitManage/secondBidsGroundItems",
              name: "addDebitItem",
              id: 1,
              "children": [

              ]
            }, {
              "title": "进行中的标管理",
              "path": "/debitManage/runningBidsItems",
              name: "addDebitItem",
              id: 1,
              "children": []
            }]
          },
          {
            "title": "复审管理",
            "path": "/debitManage/thridCon",
            id: 1,
            "children": [{
              "title": "满标复审",
              "path": "/debitManage/reexAuditItems",
              name: "addDebitItem",
              id: 1,
              "children": []
            }]
          }, {
            "title": "记录查看",
            "path": "/debitManage/viewLogs",
            id: 1,
            "children": [{
              "title": "所有借款标",
              "path": "/debitManage/debitItems",
              name: "addDebitItem",
              id: 1,
              "children": []
            }, {
              "title": "投资记录",
              "path": "/debitManage/investRecords",
              name: "addDebitItem",
              id: 1,
              "children": []
            }]
          }, {
            "title": "管理标类别",
            "path": "/debitManage/manageCategory",
            id: 1,
            "children": [{
              "title": "借款标类别",
              "path": "/debitManage/debitCategory",
              name: "addDebitItem",
              id: 1,
              "children": []
            }]
          }

        ]
      }, {
        "title": "还款管理",
        "path": "/repaymentManage",
        "icon": "el-icon-bank-card",
        id: 1,
        "children": [{
            "title": "管理还款项",
            "path": "/repaymentManage/repaymentItems",
            name: "addDebitItem",
            id: 1,
            "children": []
          },
          {
            "title": "还款记录",
            "path": "/repaymentManage/repaymentRecords",
            name: "addDebitItem",
            "children": [],
            id: 1
          }
        ]
      }, {
        "title": "资金管理",
        "path": "/capitalManage",
        id: 1,
        "icon": "el-icon-wallet",
        "children": [{
          "title": "记录查看",
          "path": "/capitalManage/logsView",
          id: 1,
          "children": [{
            "title": "充值记录",
            "path": "/capitalManage/reChargeLog",
            id: 1,
            name: "addDebitItem",
            "children": []
          }, {
            "title": "提现记录",
            "path": "/capitalManage/withDrawMoneyLog",
            id: 1,
            name: "addDebitItem",
            "children": []
          }, {
            "title": "用户资金",
            "path": "/capitalManage/userCapital",
            name: "addDebitItem",
            id: 1,
            "children": []
          }, {
            "title": "平台资金",
            "path": "/capitalManage/platformCapitalLog",
            name: "addDebitItem",
            id: 1,
            "children": []
          }]
        }, {
          "title": "相关审核",
          "path": "/capitalManage/withDrawConfirm",
          id: 1,

          "children": [{
            "title": "提现审核",
            "path": "/capitalManage/withDrawCon",
            id: 1,
            name: "addDebitItem",
            "children": []
          }]
        }]
      }, {
        "title": "会员管理",
        "path": "/memberManage",
        "icon": "el-icon-user-solid",
        id: 1,
        "children": [{
            "title": "新增借款用户",
            "path": "/memberManage/addDebitUser",
            name: "addDebitItem",
            id: 1,
            "children": []
          },
          {
            "title": "投资用户管理",
            "path": "/memberManage/investUserManage",
            name: "addDebitItem",
            id: 1,
            "children": []
          },
          {
            "title": "借款用户管理",
            "path": "/memberManage/debitUserManage",
            name: "addDebitItem",
            id: 1,
            "children": []
          }
        ]
      }, {
        "title": "内容管理",
        "path": "/contentManage",
        "icon": "el-icon-s-grid",
        id: 1,
        "children": [{
            "title": "轮播管理",
            "path": "/contentManage/sliderManage",
            id: 1,
            "children": [{
              "title": "新增轮播",
              "path": "/contentManage/sliderManage/addSliderItem",
              "children": [],
              id: 1
            }]
          },
          {
            "title": "文章管理",
            "path": "/contentManage/articleManage",
            id: 1,
            "children": [{
              "title": "新增文章",
              "path": "/contentManage/articleManage/addArticleItem",
              "children": [],
              id: 1,
            }]
          },
          {
            "title": "意见反馈",
            "path": "/contentManage/feedBack",
            id: 1,
            "children": [{
              "title": "反馈处理",
              "path": "/contentManage/feedbackManage/addFeedbackItem",
              "children": [],
              id: 1,
            }, ]
          }
        ]
      }],

父级组件:

<template>
  <div class="leftMenu">
    <el-menu
      class="el-menu-vertical-demo"
      :collapse="false"
      :router="true"
      :unique-opened="true"
      :default-active="$route.path"
      active-text-color="#ffffff"
    >
      <ActMenu :list="menuData"></ActMenu>
    </el-menu>
  </div>
</template>

<script>
import ActMenu from "@/Layout/home/menu/ActMenu";
import { mapGetters } from "vuex";
export default {
  name: "LeftMenu",
  components: { ActMenu },
  computed: {
    ...mapGetters(["getMenuData"])
  },
  data() {
    return {
      menuData: this.getMenuData
    };
  },
  created() {
    //根据用户权限获取路由,当前是管理员
    // console.log(this.$route.path);
    this.menuData = this.getMenuData;
  }
    
};
</script>

<style scoped>
.logo {
  width: 100%;
}
.logo > img {
  width: 60%;
  margin: auto;
}
.el-radio-group {
  position: absolute;
  bottom: 20px;
  left: 0;
  right: 0;
}
.leftMenu,
.leftMenu > ul {
  position: relative;
  box-sizing: border-box !important;
  overflow: hidden;
}
.leftMenu {
  position: relative;
  z-index: 10;
  width: 100%;
  height: 100%;
}
.leftMenu ul {
  box-shadow: 4px 0 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  height: 100% !important;
  background-color: rgb(33, 38, 60);
}
.el-menu > div {
  width: 200px !important;
}
</style>

 子组件ActMenu.vue:

<template>
  <div>
    <!-- 根据实际情况修改 -->
    <div v-for="item in list" :key="item.index">
      <el-menu-item
        v-if="
          item.children == null ||
            item.children.length === 0 ||
            item.path === '/home'
        "
        :index="item.path"
      >
        <i :class="item.icon"></i>
        <span>{{ item.name }}</span>
      </el-menu-item>
      <el-submenu
        v-if="
          item.children != null &&
            item.path !== '/home' &&
            item.children.length > 0
        "
        :index="item.path"
        class="secondMenu"
      >
        <template slot="title">
          <i :class="item.icon"></i>
          <span>{{ item.name }}</span>
        </template>
        <ActMenu :list="item.children"></ActMenu>
      </el-submenu>
    </div>
  </div>
</template>
<script>
export default {
  name: "ActMenu",
  props: {
    list: Array
  }
};
</script>
<style scoped>
.el-menu--inline li {
  background-color: rgb(20, 22, 34);
}
.el-menu--inline li:hover,
.el-submenu__title:hover {
  background-color: rgb(57, 62, 83) !important;
}
</style>

(其中判断可能不是很适用,要视情况修改,ui框架使用的是elementui)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你想了解如何实现一个画布侧边栏Vue 组件,是吗?那我可以给你一些思路和建议。 首先,你需要确定侧边栏的功能和布局。画布侧边栏通常用于放置一些工具、选项和设置,以便用户可以轻松地对画布进行操作和调整。因此,你需要考虑以下几个方面: 1. 侧边栏的宽度和位置:通常侧边栏会位于画布的左侧或右侧,并且需要有一个合适的宽度,以便用户可以看到所有的内容。 2. 侧边栏的内容:你需要确定侧边栏需要包含哪些组件,比如按钮、输入框、下拉框等等。这些组件需要按照一定的布局方式进行排列,以便用户可以快速找到并使用。 3. 交互和响应:侧边栏需要响应用户的操作,比如点击按钮、输入文本等等。你需要为每个组件添加对应的事件处理程序,并确保侧边栏的状态和画布的状态能够同步更新。 接下来,你可以开始编写侧边栏组件的代码了。以下是一些建议: 1. 使用 Vue组件化开发:将侧边栏拆分为多个子组件,每个组件负责一个特定的功能或布局。 2. 使用 Vuex 管理状态:将侧边栏的状态存储在 Vuex 中,以便多个组件之间可以共享和修改状态。 3. 使用插槽和作用域插槽:使用插槽可以使侧边栏更具有灵活性,可以根据需要插入不同的内容。使用作用域插槽可以将父组件的数据传递给子组件,以便子组件可以自行渲染内容。 4. 使用 CSS 样式:侧边栏的样式需要根据实际需要进行调整,你可以使用 CSS 样式来设置侧边栏的外观和布局。 最后,你可以在 Vue 应用程序中使用你的侧边栏组件了。将组件添加到应用程序的模板中,并设置需要的属性和事件处理程序即可。希望这些建议能够帮助你编写出一个功能丰富、易于使用的画布侧边栏 Vue 组件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值