前端搭建
Vue router 来动态构建左侧菜单
在views目录下创建四个页面
PageOne.vue
<template>
<h1>这是页面1</h1>
</template>
<script>
export default {
name: "PageOne",
};
</script>
<style scoped></style>
配置路由:router/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import Book from '../views/Book.vue'
import PageOne from '@/views/PageOne.vue'
import PageTwo from '@/views/PageTwo.vue'
import PageThree from '@/views/PageThree.vue'
import PageFour from '@/views/PageFour.vue'
import Nav from '@/views/Nav.vue'
import APP from '../App'
Vue.use(VueRouter)
const routes = [
{
path: "/nav1",
name: "导航1",
component: Nav,
children: [
{
path: "/nav1/pageOne",
name: "页面1",
component: PageOne
},
{
path: "/nav1/pageTwo",
name: "页面2",
component: PageTwo
}
]
},
{
path: "/nav2",
name: "导航2",
component: Nav,
children: [
{
path: "/nav2/pageThree",
name: "页面3",
component: PageThree
},
{
path: "/nav2/pageFour",
name: "页面4",
component: PageFour
}
]
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
Nav.vue
<template>
<div id="app">
<el-container style="height: 500px; border: 1px solid #eee">
<el-aside width="200px" style="background-color: rgb(238, 241, 246)">
<el-menu>
<el-submenu index="1">
<template slot="title"
><i class="el-icon-message"></i>导航一</template
>
<el-menu-item-group>
<!-- <template slot="title">分组一</template> -->
<el-menu-item index="1-1"
><router-link to="/nav1/pageOne"
>页面1</router-link
></el-menu-item
>
<el-menu-item index="1-2"
><router-link to="/nav1/pageTwo"
>页面2</router-link
></el-menu-item
>
</el-menu-item-group>
</el-submenu>
</el-menu>
<el-menu>
<el-submenu index="2">
<template slot="title"><i class="el-icon-menu"></i>导航二</template>
<el-menu-item-group>
<!-- <template slot="title">分组一</template> -->
<el-menu-item index="2-1"
><router-link to="/nav2/pageThree"
>页面3</router-link
></el-menu-item
>
<el-menu-item index="2-2"
><router-link to="/nav2/pageFour"
>页面4</router-link
></el-menu-item
>
</el-menu-item-group>
</el-submenu>
</el-menu>
<el-menu>
<el-submenu index="3">
<template slot="title"
><i class="el-icon-setting"></i>导航三</template
>
<el-menu-item-group>
<!-- <template slot="title">分组一</template> -->
<el-menu-item index="3-1">页面5</el-menu-item>
<el-menu-item index="3-2">页面6</el-menu-item>
</el-menu-item-group>
</el-submenu>
</el-menu>
</el-aside>
<el-container>
<el-header style="text-align: right; font-size: 12px">
<el-dropdown>
<i class="el-icon-setting" style="margin-right: 15px"></i>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item>查看</el-dropdown-item>
<el-dropdown-item>新增</el-dropdown-item>
<el-dropdown-item>删除</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
<span>王小虎</span>
</el-header>
<!-- 内容 -->
<el-main>
<router-view></router-view>
</el-main>
</el-container>
</el-container>
</div>
</template>
<style>
.el-header {
background-color: #b3c0d1;
color: #333;
line-height: 60px;
}
.el-aside {
color: #333;
}
</style>
<script>
export default {
name: "App",
};
</script>
App.vue
<template>
<div id="app">
<router-view />
</div>
</template>
<script>
export default {
name: "App",
};
</script>
<style lang="scss"></style>
main.js
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import VueRouter from 'vue-router'
//ElementUI
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import Vuex from 'vuex';
Vue.use(ElementUI);
Vue.use(VueRouter);
Vue.use(Vuex);
Vue.prototype.axios = axios;
Vue.config.productionTip = false
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
页面展示:
后端代码同上篇:基于Springboot+Vue的前后端分离的简单Demo案例(一)-CSDN博客