Appearance
组件方式
<!--
Access control component for fine-grained access control.
-->
<script lang="ts">
import type { PropType } from 'vue';
import { defineComponent } from 'vue';
import { RoleEnum } from '/@/enums/roleEnum';
import { usePermission } from '/@/hooks/web/usePermission';
import { getSlot } from '/@/utils/helper/tsxHelper';
export default defineComponent({
name: 'Authority',
props: {
/**
* Specified role is visible
* When the permission mode is the role mode, the value value can pass the role value.
* When the permission mode is background, the value value can pass the code permission value
* @default ''
*/
value: {
type: [Number, Array, String] as PropType<RoleEnum | RoleEnum[] | string | string[]>,
default: '',
},
},
setup(props, { slots }) {
const { hasPermission } = usePermission();
/**
* Render role button
*/
function renderAuth() {
const { value } = props;
if (!value) {
return getSlot(slots);
}
return hasPermission(value) ? getSlot(slots) : null;
}
return () => {
// Role-based value control
return renderAuth();
};
},
});
</script>
tsxHelper.tsx
import { Slots } from 'vue';
import { isFunction } from '/@/utils/is';
/**
* @description: Get slot to prevent empty error
*/
export function getSlot(slots: Slots, slot = 'default', data?: any) {
if (!slots || !Reflect.has(slots, slot)) {
return null;
}
if (!isFunction(slots[slot])) {
console.error(`${slot} is not a function!`);
return null;
}
const slotFn = slots[slot];
if (!slotFn) return null;
return slotFn(data); //
}
使用
<Authority :value="'1000'">
<a-button type="primary" class="mx-4"> 拥有code ['1000']权限可见 </a-button>
</Authority>
<Authority :value="'2000'">
<a-button color="success" class="mx-4"> 拥有code ['2000']权限可见 </a-button>
</Authority>
<Authority :value="['1000', '2000']">
<a-button color="error" class="mx-4"> 拥有code ['1000','2000']角色权限可见 </a-button>
</Authority>
指令方式
/**
* Global authority directive
* Used for fine-grained control of component permissions
* @Example v-auth="RoleEnum.TEST"
*/
import type { App, Directive, DirectiveBinding } from 'vue';
import { usePermission } from '/@/hooks/web/usePermission';
function isAuth(el: Element, binding: any) {
const { hasPermission } = usePermission();
const value = binding.value;
if (!value) return;
if (!hasPermission(value)) {
el.parentNode?.removeChild(el);
}
}
const mounted = (el: Element, binding: DirectiveBinding<any>) => {
isAuth(el, binding);
};
const authDirective: Directive = {
mounted,
};
export function setupPermissionDirective(app: App) {
app.directive('auth', authDirective);
}
export default authDirective;
使用
<a-button v-auth="'1000'" type="primary" class="mx-4"> 拥有code ['1000']权限可见 </a-button>
<a-button v-auth="'2000'" color="success" class="mx-4"> 拥有code ['2000']权限可见 </a-button>
<a-button v-auth="['1000', '2000']" color="error" class="mx-4">
拥有code ['1000','2000']角色权限可见
</a-button>