Skip to content

Commit

Permalink
fix[v-permission]: support dynamic set roles (#3251)
Browse files Browse the repository at this point in the history
  • Loading branch information
PanJiaChen committed Jun 15, 2020
1 parent e9419b6 commit bee999f
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/directive/permission/permission.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import store from '@/store'

export default {
inserted(el, binding, vnode) {
const { value } = binding
const roles = store.getters && store.getters.roles
function checkPermission(el, binding) {
const { value } = binding
const roles = store.getters && store.getters.roles

if (value && value instanceof Array && value.length > 0) {
if (value && value instanceof Array) {
if (value.length > 0) {
const permissionRoles = value

const hasPermission = roles.some(role => {
Expand All @@ -15,8 +15,17 @@ export default {
if (!hasPermission) {
el.parentNode && el.parentNode.removeChild(el)
}
} else {
throw new Error(`need roles! Like v-permission="['admin','editor']"`)
}
} else {
throw new Error(`need roles! Like v-permission="['admin','editor']"`)
}
}

export default {
inserted(el, binding) {
checkPermission(el, binding)
},
update(el, binding) {
checkPermission(el, binding)
}
}

0 comments on commit bee999f

Please sign in to comment.