父组件
<template>
<div>
<el-menu
:router="true"
background-color="#001529"
text-color="#fff"
class="navleft"
:default-active="this.$route.path"
unique-opened>
<!-- 先循环数组,然后把每一项的值给传到子组件内 因为不能直接传数组,要传值 -->
<menus v-for="(item,index) in menuData" :key="index" :menu="item" />
</el-menu>
</div>
</template>
<script>
import menus from "./navitem";
export default {
data() {
return {
menuData: [
{
id: 1,
path: "/a",
name: "AdminIndex",
nameZh: "首页",
iconCls: "el-icon-s-home",
component: "AdminIndex",
parentId: 0,
children: null
},
{
id: 2,
path: "/admin",
name: "User",
nameZh: "用户管理",
iconCls: "el-icon-user",
component: "AdminIndex",
parentId: 0,
children: [
{
id: 6,
path: "/admin/user/profile",
name: "Profile",
nameZh: "用户信息",
iconCls: null,
component: "user/UserProfile",
parentId: 2,
children: null
},
{
id: 7,
path: "/admin/user/role",
name: "Role",
nameZh: "角色配置",
iconCls: null,
component: "user/Role",
parentId: 2,
children: null
}
]
},
{
id: 3,
path: "/d",
name: "Content",
nameZh: "内容管理",
iconCls: "el-icon-tickets",
component: "AdminIndex",
parentId: 0,
children: [
{
id: 9,
path: "/d/content/department",
name: "Department",
nameZh: "就业部类",
iconCls: null,
component: "content/department",
parentId: 3,
children: null
},
{
id: 10,
path: "/d/content/student",
name: "Student",
nameZh: "学生类",
iconCls: null,
component: "content/student",
parentId: 3,
children: [
{
id: 20,
path: "/d/content/student/graduateInfo",
name: "graduateInfo",
nameZh: "毕业生信息",
iconCls: null,
component: "content/student/graduateInfo",
parentId: 10,
children: null
}
]
},
{
id: 11,
path: "/d/content/enterprise",
name: "Enterprise",
nameZh: "企业类",
iconCls: null,
component: "content/enterprise",
parentId: 3,
children: null
}
]
},
{
id: 4,
path: "/n",
name: "System",
nameZh: "系统管理",
iconCls: "el-icon-s-tools",
component: "AdminIndex",
parentId: 0,
children: [
{
id: 12,
path: "/n/system/run",
name: "Run",
nameZh: "运行情况",
iconCls: null,
component: "system/run",
parentId: 4,
children: null
},
{
id: 13,
path: "/n/system/data",
name: "Data",
nameZh: "备份恢复数据库",
iconCls: null,
component: "system/data",
parentId: 4,
children: null
},
{
id: 14,
path: "/n/system/log",
name: "Log",
nameZh: "操作日志",
iconCls: null,
component: "system/log",
parentId: 4,
children: null
}
]
},
{
id: 5,
path: "/admin1",
name: "Link",
nameZh: "链接",
iconCls: null,
component: "AdminIndex",
parentId: 0,
children: null
}
]
};
},
components: {
menus
}
};
</script>
<style lang="scss" scoped>
.navleft {
overflow: hidden;
}
.navtitle {
color: #fff;
font-size: 18px;
}
/deep/.el-menu {
border-right: none;
}
</style>
子组件
<template>
<div>
<!-- 无子集 -->
<!-- index放url可以在路由开始后直接点击跳转,不需要在一个个设置
因为这边是数据path对应的跳转路由
-->
<el-menu-item v-if="!menu.children" :index="menu.path">
<!-- <i :class="menu.icon"></i> -->
<span slot="title">{
{
menu.name}}</span>
</el-menu-item>
<!-- 有子集 -->
<!-- index放url可以在路由开始后直接点击跳转,不需要在一个个设置
因为这边是数据path对应的跳转路由
-->
<el-submenu v-else :index="menu.path">
<template slot="title">
<i :class="menu.icon"></i>
<span>{
{
menu.name}}</span>
</template>
<menus v-for="(item,index) in menu.children" :menu="item" :key="index"></menus>
</el-submenu>
</div>
</template>
<script>
export default {
name: "menus",
props: ["menu"]
};
</script>
<style lang="scss" scoped>
</style>
参考链接