This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
handingkang-ohmyweb/src/directives/permission/index.ts

22 lines
629 B
TypeScript
Raw Normal View History

import { useUserStoreHook } from '@/store/modules/user'
import { Directive } from 'vue'
/** 权限指令 */
export const permission: Directive = {
mounted(el, binding) {
const { value } = binding
const roles = useUserStoreHook().roles
if (value && value instanceof Array && value.length > 0) {
const permissionRoles = value
const hasPermission = roles.some((role: any) => {
return permissionRoles.includes(role)
})
if (!hasPermission) {
el.style.display = 'none'
}
} else {
throw new Error("need roles! Like v-permission=\"['admin','editor']\"")
}
}
}