permission.ts 742 B

1234567891011121314151617181920212223242526272829303132
  1. /**
  2. * Global authority directive
  3. * Used for fine-grained control of component permissions
  4. * @Example v-auth="RoleEnum.TEST"
  5. */
  6. import type { App, Directive, DirectiveBinding } from 'vue';
  7. import { usePermission } from '/@/hooks/web/usePermission';
  8. function isAuth(el: Element, binding: any) {
  9. const { hasPermission } = usePermission();
  10. const value = binding.value;
  11. if (!value) return;
  12. if (!hasPermission(value)) {
  13. el.parentNode?.removeChild(el);
  14. }
  15. }
  16. const mounted = (el: Element, binding: DirectiveBinding<any>) => {
  17. isAuth(el, binding);
  18. };
  19. const authDirective: Directive = {
  20. mounted,
  21. };
  22. export function setupPermissionDirective(app: App) {
  23. app.directive('auth', authDirective);
  24. }
  25. export default authDirective;