🌺前言
vue3使用vue自定义指令控制按钮权限
typescript
// 引入vue中定义的指令对应的类型定义
import { usePermissionStore } from '@/store/permission';
import { Directive } from 'vue';
export const permission: Directive = {
// mounted是指令的一个生命周期
mounted(el, binding) {
// value 获取用户使用自定义指令绑定的内容
const { value } = binding;
// 获取用户所有的权限按钮
const permissionStore = usePermissionStore();
// 判断用户使用自定义指令,是否使用正确了
if (value && value instanceof Array && value.length > 0) {
const isHasPermission = permissionStore.hasPermission(value);
// 当用户没有这个按钮权限时,设置隐藏这个按钮
if (!isHasPermission) {
el.parentNode.removeChild(el);
}
} else {
throw new Error(
"need roles! Like v-permission=\"['admin','editor']\"",
);
}
},
};
文章最后更新于 2024-08-24 17:08:24
作者:徐徐版权声明:转载请注明文章出处
留言

~~空空如也