Files
cdTestPlant3/cdTMP/src/directives/role/index.js
2024-09-03 17:40:15 +08:00

31 lines
783 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import role from "./role"
// 该文件规定如何role不为对应值则删除dom
// 用法vue的指令 -> v-role="['admin']"即只允许userStore里面role属性为admin才能看见
const checkRole = (el, binding) => {
const { value } = binding
if (Array.isArray(value)) {
if (value.length > 0) {
let isHas = false
value.map((item) => {
isHas = role(item)
})
if (!isHas && el.parentNode) {
el.parentNode.removeChild(el)
}
}
} else {
throw new Error(`need role! Like v-role="['seo', 'cfo']"`)
}
}
export default {
mounted(el, binding) {
checkRole(el, binding)
},
updated(el, binding) {
checkRole(el, binding)
}
}