UPDATE:优化v-auth指令逻辑

This commit is contained in:
贾同学
2025-08-20 10:15:09 +08:00
parent a2db45cace
commit 26647222e2

View File

@@ -6,21 +6,24 @@ import { useAuthStore } from '@/stores/modules/auth'
import type { Directive, DirectiveBinding } from 'vue' import type { Directive, DirectiveBinding } from 'vue'
const auth: Directive = { const auth: Directive = {
mounted(el: HTMLElement, binding: DirectiveBinding) { mounted(el: HTMLElement, binding: DirectiveBinding) {
const { value } = binding //console.log('binding',binding)
const authStore = useAuthStore() const { value, modifiers } = binding
const currentPageRoles = authStore.authButtonListGet[authStore.routeName] ?? [] let currentPageRoles = []
// console.log('1234',authStore.routeName) const authStore = useAuthStore()
// console.log('123',currentPageRoles) if (modifiers && Object.keys(modifiers).length) {
if (value instanceof Array && value.length) { currentPageRoles = authStore.authButtonListGet[Object.keys(modifiers)[0]] ?? []
//console.log('123456',value) } else {
const hasPermission = value.every(item => currentPageRoles.includes(item)) currentPageRoles = authStore.authButtonListGet[authStore.routeName] ?? []
if (!hasPermission) el.remove() }
} else { console.log('currentPageRoles', currentPageRoles)
//console.log('12345',value) if (value instanceof Array && value.length) {
if (!currentPageRoles.includes(value)) el.remove() const hasPermission = value.every(item => currentPageRoles.includes(item))
if (!hasPermission) el.remove()
} else {
if (!currentPageRoles.includes(value)) el.remove()
}
} }
},
} }
export default auth export default auth