perf: 代码优化 directives/permission
This commit is contained in:
parent
94d96c95e7
commit
9618a17ab5
@ -5,14 +5,15 @@ import { useUserStoreHook } from "@/store/modules/user"
|
|||||||
export const permission: Directive = {
|
export const permission: Directive = {
|
||||||
mounted(el, binding) {
|
mounted(el, binding) {
|
||||||
const { value } = binding
|
const { value } = binding
|
||||||
const roles = useUserStoreHook().roles
|
const { roles } = useUserStoreHook()
|
||||||
if (value && value instanceof Array && value.length > 0) {
|
if (Array.isArray(value) && value.length > 0) {
|
||||||
const permissionRoles = value
|
const permissionRoles = value
|
||||||
const hasPermission = roles.some((role) => {
|
const hasPermission = roles.some((role) => permissionRoles.includes(role))
|
||||||
return permissionRoles.includes(role)
|
|
||||||
})
|
|
||||||
if (!hasPermission) {
|
if (!hasPermission) {
|
||||||
el.style.display = "none"
|
// 隐藏
|
||||||
|
// el.style.display = "none"
|
||||||
|
// 销毁
|
||||||
|
el.parentNode?.removeChild(el)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`need roles! Like v-permission="['admin','editor']"`)
|
throw new Error(`need roles! Like v-permission="['admin','editor']"`)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user