vue+element-ui 实现多级侧边栏导航

本文介绍了如何结合Vue.js和Element-UI库来实现一个多级的侧边栏导航菜单。通过封装asideBarItem组件和配置路由,实现了动态展示各级导航项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@效果图
在这里插入图片描述

导航菜单

<!--导航菜单-->
<el-menu
	router
	:default-active="$route.path"
	v-show="!collapsed"
	unique-opened>
	<!-- 左侧导航栏抽取循环部分 将路由列表传给子组件-->
	<asideBarItem
		v-for="(router,index) in $router.options.routes"
		:router="router"
		:index="index"
		:key="index"
		v-if="!router.hidden"
	>
	</asideBarItem>
</el-menu>

asideBarItem组件封装

<template>
  <div class="asideBarItem-container">
      <el-submenu :index="index+''" v-if="!router.hidden&&router.children&&router.children.length>0">
      	<span slot="title"><i :class="router.iconCls"></i>{{router.name}}</span>
        <asideBarItem v-for="(child, childKey) in router.children" :key="child.path" :router="child" :index="index+'-'+childKey"></asideBarItem>
      </el-submenu>
      <el-menu-item
        v-if="!router.hidden&&(!router.children||router.children.length==0)"
        :key="router.path"
        :index="router.path">
        <span slot="title"><i :class="router.iconCls"></i>{{router.name}}</span>
      </el-menu-item>
  </div>
</template>

<script>
  export default {
    name: 'asideBarItem',
    props: [
      "router",
      "index"
    ],
    components: {},
    data() {
      return {}
    },
    computed: {},
    watch: {},
    methods: {},
    created() {},
    mounted() {}
  }
</script>

<style lang="scss" scoped>
  .asideBarItem-container {
  }
</style>

路由

routes: [
    {
        path: '/',
        component: List,
        name: '列表',
        //iconCls: 'el-icon-list',//图标样式class
        children: [
            { path: '/room', component:Room, name: '客房' ,children:[{
            		path: '/test', component:Test,  name: '登录'
           		}]
            },
            { path: '/spring', component: Spring,  name: '温泉' },
            { path: '/foodie', component: Foodie, name: '餐饮' }
        ]
    },
    {
        path: '/login',
        component: Login,
        name: 'Login',
        hidden: true
    }
]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值