目录
1、侧边栏组件
1.1 侧边栏组件
1.2、getters.js
1.3、permission 模块
1.4、app 模块
1.5、vuex中添加新模块
1.6、样式文件
1.7、变量scss文件
2、SidebarItem 组件
2.1 SidebarIntem 组件
2.2、验证工具类
2.3、菜单项组件
2.4 链接组件
在完成 <首页-获取路由信息> 和 <首页-svg图标> 这两章后,本章将之前的<主页布局-左侧导航菜单(静态)>改造为动态菜单
效果图:
原来静态菜单中,我们是写死的,现在需要改造成动态生成组件的形式
修改 src / layout / components / Sidebar / index.vue
修改 src / store / getters.js 文件
const getters = {
roles: state => state.user.roles,
permission_routes: state => state.permission.routes,
sidebar: state => state.app.sidebar
}
export default getters
新建 src / store / modules / permission.js 文件
import router from '@/router'
import { getRouters } from '@/api/menu'
import Layout from '@/layout/index'
const permission = {
state: {
routes: [],
addRoutes: []
},
mutations: {
SET_ROUTES: (state, routes) => {
state.addRoutes = routes
state.routes = router.options.routes.concat(routes)
}
},
actions: {
// 生成路由
GenerateRoutes({ commit }) {
return new Promise(resolve => {
// 向后端请求路由数据
getRouters().then(res => {
const accessedRoutes = filterAsyncRouter(res.data)
accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes)
})
})
}
}
}
// 遍历后台传来的路由字符串,转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
return asyncRouterMap.filter(route => {
if (route.component) {
// Layout组件特殊处理
if (route.component === 'Layout') {
route.component = Layout
} else {
route.component = loadView(route.component)
}
}
if (route.children != null && route.children && route.children.length) {
route.children = filterAsyncRouter(route.children)
}
return true
})
}
export const loadView = (view) => { // 路由懒加载
// return () => import(`@/views/${view}`) 这种写法会误报: eslint Cannot read property 'range' of null
return () => import('@/views/' + view)
}
export default permission
新建 src / store / modules / app.js 文件
import Cookies from 'js-cookie'
const state = {
sidebar: {
opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
withoutAnimation: false
}
}
const mutations = {
TOGGLE_SIDEBAR: state => {
state.sidebar.opened = !state.sidebar.opened
state.sidebar.withoutAnimation = false
if (state.sidebar.opened) {
Cookies.set('sidebarStatus', 1)
} else {
Cookies.set('sidebarStatus', 0)
}
},
CLOSE_SIDEBAR: (state, withoutAnimation) => {
Cookies.set('sidebarStatus', 0)
state.sidebar.opened = false
state.sidebar.withoutAnimation = withoutAnimation
}
}
const actions = {
toggleSideBar({ commit }) {
commit('TOGGLE_SIDEBAR')
},
closeSideBar({ commit }, { withoutAnimation }) {
commit('CLOSE_SIDEBAR', withoutAnimation)
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
修改 src / store / index.js 文件
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import getters from './getters'
import permission from './modules/permission'
import app from './modules/app'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
},
mutations: {
},
actions: {
},
modules: {
user,
permission,
app
},
getters
})
新增 src / assets / styles / sidebar.scss 文件
#app {
.main-container {
min-height: 100%;
transition: margin-left .28s;
margin-left: $sideBarWidth;
position: relative;
}
.sidebar-container {
width: $sideBarWidth !important;
background-color: $menuBg;
height: 100%;
position: fixed;
font-size: 0px;
top: 0;
bottom: 0;
left: 0;
overflow: hidden;
.scrollbar-wrapper {
height: 100%;
overflow-x: hidden !important;
.el-scrollbar__wrap {
overflow-x: hidden !important;
}
a {
display: inline-block;
width: 100%;
overflow: hidden;
text-decoration: none;
}
.el-menu {
border: none;
height: 100%;
width: 100% !important;
}
.svg-icon {
margin-right: 16px;
}
}
}
}
修改 src / assets / styles / variables.scss 文件
// sidebar
$sideBarWidth: 200px;
$menuBg:#304156;
$menuText:#bfcbd9;
$menuActiveText:#409EFF;
:export {
sideBarWidth: $sideBarWidth;
menuBg: $menuBg;
menuText: $menuText;
menuActiveText: $menuActiveText;
}
SidebarItem 组件是对 el-menu-item 和 el-submenu 的封装,当遍历路由项时,我们需要判断一下该条路由有没有子路由,如果有子路由我们就用 el-submenu 来渲染, 如果该条路由没有子路由,则用 el-menu-item 来渲染
新建 src / layout / components / Sidebar / SidebarIntem.vue 文件
新建 src / utils / validate.js 文件
/**
* @param {string} path
* @returns {Boolean}
*/
export function isExternal(path) {
return /^(https?:|mailto:|tel:)/.test(path)
}
新建 src / layout / componnents / Sidebar / Item.vue 文件
新建 src / layout / componnents / Sidebar / Link.vue 文件