checkPermission.js 951 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-11-01 10:33:45
  4. * @LastEditTime: 2021-12-28 11:34:01
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: \Foshan4A\src\config\checkPermission.js
  8. */
  9. import Vue from 'vue'
  10. import store from '@/store'
  11. function checkPermission (el, binding) {
  12. const { value } = binding
  13. const roles = store.getters && store.getters.roles
  14. if (value && value instanceof Array) {
  15. if (value.length > 0) {
  16. const permissionRoles = value
  17. const hasPermission = roles.some(role => {
  18. return permissionRoles.includes(role)
  19. })
  20. if (!hasPermission) {
  21. el.parentNode && el.parentNode.removeChild(el)
  22. }
  23. }
  24. } else {
  25. throw new Error(`need roles! Like v-is="['admin','editor']"`)
  26. }
  27. }
  28. Vue.directive(
  29. 'is', {
  30. inserted (el, binding) {
  31. checkPermission(el, binding)
  32. },
  33. update (el, binding) {
  34. checkPermission(el, binding)
  35. }
  36. }
  37. )