菜单权限实现
import Layout from '@/layout'
const constantRoutes = [
{
path: '/login',
component: () => import('@/views/login/index'),
hidden: true
},
{
path: '/404',
component: () => import('@/views/404'),
hidden: true
},
{
path: '/',
component: Layout,
redirect: '/dashboard',
children: [{
path: 'dashboard',
name: 'Dashboard',
component: () => import('@/views/dashboard/index'),
meta: {title: '首页', icon: 'dashboard'}
}]
}
]
const anyRoutes = {path: '*', redirect: '/404', hidden: true}
const asyncRoutes = [
{
name: 'Test',
path: '/test',
component: Layout,
redirect: {name: 'test2'},
meta: {
title: '测试',
icon: 'el-icon-s-shop'
},
children: [
{
name: 'test2',
path: 'test2/list',
component: () => import( '@/views/product/test2/list'),
meta: {title: '测试2'}
},
{
name: 'test3',
path: 'test3/list',
component: () => import( '@/views/product/test3/list'),
meta: {
title: '测试3'
}
},
}
]
function handleBaseRouterToRolesRouter (asyncRoutes, routeNames) {
let routes = asyncRoutes.filter(item => {
return routeNames.some(r => {
return r === item.name
})
})
for (const item of routes) {
if (item.children) {
item.children = handleBaseRouterToRolesRouter(item.children, routeNames)
}
}
return routes
}
router.addRoutes([...handleBaseRouterToRolesRouter(asyncRoutes,'后台返回的权限数据'), anyRoutes])
按钮权限实现
export function hasBtnPermission(permission) {
const myBtns = store.getters.buttons
return myBtns.indexOf(permission) !== -1
}