vue-admin-template 动态路由的实现(方式二 推荐),成功跳槽百度工资从15K涨到28K
将动态显示的路由写在asyncRoutes 中并添加 roles ,例如meta: { roles: [‘admin’] },
- 在store/modulds目录下添加permission.js
import { asyncRoutes, constantRoutes } from ‘@/router’
/**
- Use meta.role to determine if the current user has permission
- @param roles
- @param route
*/
function hasPermission(roles, route) {
if (route.meta && route.meta.roles) {
return roles.some(role => route.meta.roles.includes(role))
} else {
return true
}
}
/**
- Filter asynchronous routing tables by recursion
- @param routes asyncRoutes
- @param roles
*/
export function filterAsyncRoutes(routes, roles) {
const res = []
routes.forEach(route => {
const tmp = { …route }
if (hasPermission(roles, tmp)) {
if (tmp.children) {
tmp.children = filterAsyncRoutes(tmp.children, roles)
}
res.push(tmp)
}
})
return res
}
const state = {
routes: [],
addRoutes: []
}
const mutations = {
SET_ROUTES: (state, routes) => {
state.addRoutes = routes
state.routes = constantRoutes.concat(routes)
}
}
const actions = {
generateRoutes({ commit }, roles) {
return new Promise(resolve => {
const accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
commit(‘SET_ROUTES’, accessedRoutes)
resolve(accessedRoutes)
})
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
- 修改store/modulds/user.js
import { login, logout, getInfo } from ‘@/api/user’
import { getToken, setToken, removeToken } from ‘@/utils/auth’
import { resetRouter } from ‘@/router’
const getDefaultState = () => {
return {
token: getToken(),
name: ‘’,
avatar: ‘’,
roles: []
}
}
const state = getDefaultState()
const mutations = {
RESET_STATE: (state) => {
Object.assign(state, getDefaultState())
},
SET_TOKEN: (state, token) => {
state.token = token
},
SET_NAME: (state, name) => {
state.name = name
},
SET_AVATAR: (state, avatar) => {
state.avatar = avatar
},
SET_ROLES: (state, roles) => {
state.roles = roles
}
}
const actions = {
// user login
login({ commit }, userInfo) {
const { username, password } = userInfo
return new Promise((resolve, reject) => {
login({ username: username.trim(), password: password }).then(response => {
const { data } = response
commit(‘SET_TOKEN’, data.token)
setToken(data.token)
resolve()
}).catch(error => {
reject(error)
})
})
},
// get user info
getInfo({ commit, state }) {
return new Promise((resolve, reject) => {
getInfo(state.token).then(response => {
const { data } = response
if (!data) {
return reject(‘验证失败,请重新登录’)
}
const {roles, name, avatar } = data
commit(‘SET_ROLES’, roles)
commit(‘SET_NAME’, name)
commit(‘SET_AVATAR’, avatar)
resolve(data)
}).catch(error => {
reject(error)
})
})
},
// user logout
logout({ commit, state }) {
return new Promise((resolve, reject) => {
logout(state.token).then(() => {
removeToken() // must remove token first
resetRouter()
commit(‘RESET_STATE’)
commit(‘SET_ROLES’, [])
resolve()
}).catch(error => {
reject(error)
})
})
},
// remove token
resetToken({ commit }) {
return new Promise(resolve => {
removeToken() // must remove token first
commit(‘RESET_STATE’)
commit(‘SET_ROLES’, [])
resolve()
})
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
添加roles: [] 保存权限列表 ,添加内容如下
const getDefaultState = () => {
return {
…
roles: []
}
}
const mutations = {
…
SET_ROLES: (state, roles) => {
state.roles = roles
}
}
// get user info
getInfo({ commit, state }) {
return new Promise((resolve, reject) => {
getInfo(state.token).then(response => {
结尾
正式学习前端大概 3 年多了,很早就想整理这个书单了,因为常常会有朋友问,前端该如何学习,学习前端该看哪些书,我就讲讲我学习的道路中看的一些书,虽然整理的书不多,但是每一本都是那种看一本就秒不绝口的感觉。
以下大部分是我看过的,或者说身边的人推荐的书籍,每一本我都有些相关的推荐语,如果你有看到更好的书欢迎推荐呀。
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
tions = {
…
SET_ROLES: (state, roles) => {
state.roles = roles
}
}
// get user info
getInfo({ commit, state }) {
return new Promise((resolve, reject) => {
getInfo(state.token).then(response => {
结尾
正式学习前端大概 3 年多了,很早就想整理这个书单了,因为常常会有朋友问,前端该如何学习,学习前端该看哪些书,我就讲讲我学习的道路中看的一些书,虽然整理的书不多,但是每一本都是那种看一本就秒不绝口的感觉。
以下大部分是我看过的,或者说身边的人推荐的书籍,每一本我都有些相关的推荐语,如果你有看到更好的书欢迎推荐呀。
更多推荐
所有评论(0)