Skip to content

Instantly share code, notes, and snippets.

@chhumsina
Created May 4, 2020 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chhumsina/43bd9a1bc6628dbe7b95d53de0657b5a to your computer and use it in GitHub Desktop.
Save chhumsina/43bd9a1bc6628dbe7b95d53de0657b5a to your computer and use it in GitHub Desktop.
Check permission directive for nuxtjs
import Vue from 'vue'
export default function({ store }, inject) {
Vue.directive('permission', {
inserted(el, binding, vnode) {
const { permission } = binding
const permissionList = store.state.auth.user.permission
if (permission && Array.isArray(permission) && permission.length > 0) {
const hasPermission = permissionList.some((role) => {
return permission.includes(role)
})
if (!hasPermission) {
el.parentNode && el.parentNode.removeChild(el)
}
} else {
throw new Error(
`need permission! Like v-permission="['view_user','edit_user']"`
)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment