Created
May 4, 2020 04:02
-
-
Save chhumsina/43bd9a1bc6628dbe7b95d53de0657b5a to your computer and use it in GitHub Desktop.
Check permission directive for nuxtjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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