1.登录成功后,后台返回permissions 数组,localStorage存菜单权限(或者按钮权限)
localStorage.setItem('permissions', (res.permissions || []).join(','))
2.新增判断是否有权限的方法,随便写在哪个js中都可以
export function hasPermissions(path) {
if (path === '/' || path === '/dashboard') { //默认都拥有首页的权限
return true
}
const hasPermissions = localStorage.getItem("permissions") || ''
if (!hasPermissions || hasPermissions === '') { // 为空默认拥有全部权限
return true
}
path = path.replaceAll('//', '/') // 替换调 //
return (hasPermissions.split(',').indexOf(path) > -1)
}
3.菜单权限 sidebar/index.vue
没权限的 hidden = true
routes() {
const rs = this.$router.options.routes
for (let i = 0, li = rs.length; i < li; i++) {
rs[i].hidden = !hasPermissions(rs[i].path)
if (rs[i].children && rs[i].children.length > 0) {
for (let j = 0, lj = rs[i].children.length; j < lj; j++) {
rs[i].children[j].hidden = !hasPermissions(rs[i].path + '/' + rs[i].children[j].path)
}
}
}
return rs
}
4.路由权限 permission.js
适当位置加上 没权限的 跳到首页 (防止直接输入url)
if (!hasPermissions(to.path)) {
next({path: '/'})
NProgress.done()
return
}
以上是菜单权限(菜单是否显示)跟路由权限(防止直接输入url)
5.按钮权限
这个简单了 <el-button v-if='hasPermissions("菜单权限url")' />
网友评论