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'
const auth: Directive = {
mounted(el: HTMLElement, binding: DirectiveBinding) {
const { value } = binding
const authStore = useAuthStore()
const currentPageRoles = authStore.authButtonListGet[authStore.routeName] ?? []
// console.log('1234',authStore.routeName)
// console.log('123',currentPageRoles)
if (value instanceof Array && value.length) {
//console.log('123456',value)
const hasPermission = value.every(item => currentPageRoles.includes(item))
if (!hasPermission) el.remove()
} else {
//console.log('12345',value)
if (!currentPageRoles.includes(value)) el.remove()
mounted(el: HTMLElement, binding: DirectiveBinding) {
//console.log('binding',binding)
const { value, modifiers } = binding
let currentPageRoles = []
const authStore = useAuthStore()
if (modifiers && Object.keys(modifiers).length) {
currentPageRoles = authStore.authButtonListGet[Object.keys(modifiers)[0]] ?? []
} else {
currentPageRoles = authStore.authButtonListGet[authStore.routeName] ?? []
}
console.log('currentPageRoles', currentPageRoles)
if (value instanceof Array && value.length) {
const hasPermission = value.every(item => currentPageRoles.includes(item))
if (!hasPermission) el.remove()
} else {
if (!currentPageRoles.includes(value)) el.remove()
}
}
},
}
export default auth