Element-ui实现左侧二级导航
这里使用的Element文档版本是2.13.1。
路由文件index.js:
import Vue from 'vue'
import Router from 'vue-router'
import Form from '@/components/Form'
import Data from '@/components/Data'
import Radio from '@/components/Radio'
import Checkbox from '@/components/Checkbox'
import Table from '@/components/Table'
import Tag from '@/components/Tag'
import Button from '@/components/Button'
import Tabs from '@/components/Tabs'
import Echarts from '@/components/Echarts'
Vue.use(Router);
let router = new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'index',
redirect: '/form/radio'
},
{
path: '/form',
name: 'form',
component: Form,
meta: {
title: 'Form',
icon: 'el-icon-eleme'
},
children: [
{
path: '/form/radio',
name: 'radio',
component: Radio,
meta: {
title: 'Radio'
}
},
{
path: '/form/checkbox',
name: 'checkbox',
component: Checkbox,
meta: {
title: 'Checkbox'
}
}
]
},
{
path: '/data',
name: 'data',
component: Data,
meta: {
title: 'Data',
icon: 'el-icon-upload'
},
children: [
{
path: '/data/table',
name: 'table',
component: Table,
meta: {
title: 'Table'
}
},
{
path: '/data/tag',
name: 'tag',
component: Tag,
meta: {
title: 'Tag'
}
}
]
},
{
path: '/button',
name: 'button',
component: Button,
meta: {
title: 'Button',
icon: 'el-icon-s-order'
}
},
{
path: '/tabs',
name: 'tabs',
component: Tabs,
meta: {
title: 'Tabs',
icon: 'el-icon-s-ticket'
}
},
{
path: '/echarts',
name: 'echarts',
component: Echarts,
meta: {
title: 'Echarts',
icon: 'el-icon-s-marketing'
}
}
]
});
export default router
App.vue:
<template>
<div id="app">
<el-container style="height: 100%;">
<el-header style="height: 80px;">Header</el-header>
<el-container>
<el-aside width="210px">
<el-row class="tac">
<el-col :span="24">
<el-menu
default-active="2"
class="el-menu-vertical-demo"
@open="handleOpen"
@close="handleClose"
>
<template v-for="route in this.$router.options.routes">
<el-submenu
:key="route.path"
:index="route.path"
v-if="route.children && route.children.length"
>
<template slot="title">
<i :class="route.meta.icon"></i>
<span>{{route.meta.title}}</span>
</template>
<el-menu-item-group>
<router-link :to="todo.path" :key="todo.path" v-for="todo in route.children">
<el-menu-item
:index="todo.path"
>{{todo.meta.title}}</el-menu-item>
</router-link>
</el-menu-item-group>
</el-submenu>
<router-link
:to="route.path"
:key="route.path"
v-else-if="!route.children && route.path != '/'"
>
<el-menu-item :index="route.path">
<i :class="route.meta.icon"></i>
<span>{{route.meta.title}}</span>
</el-menu-item>
</router-link>
</template>
</el-menu>
</el-col>
</el-row>
</el-aside>
<el-main>
<router-view></router-view>
</el-main>
</el-container>
</el-container>
</div>
</template>
<script>
export default {
name: "App",
components: {},
methods: {
handleOpen(key, keyPath) {
console.log(key, keyPath);
},
handleClose(key, keyPath) {
console.log(key, keyPath);
}
}
};
</script>
<style>
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
height: 100%;
}
.el-header,
.el-footer {
background-color: #b3c0d1;
color: #333;
line-height: 80px;
}
.el-aside {
background-color: #d3dce6;
color: #333;
}
.el-aside a{
text-decoration: none;
}
.el-main {
background-color: #e9eef3;
color: #333;
}
body > .el-container {
margin-bottom: 40px;
}
.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
line-height: 260px;
}
.el-container:nth-child(7) .el-aside {
line-height: 320px;
}
</style>
其中'<el-menu>'中的'<router-link></router-link>'是关键,不然没法导航到相关组件。
Form.vue、Data.vue组件模板中需要添加'<router-view></router-view>'。