15815213711
2022-03-07 56f8b51c26bd1fb7e1fdc62acab5151cdf83c860
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * v-hasRole 角色权限处理
 */
import store from "@/store";
import {Directive} from "@vue/runtime-core";
 
export const hasRole: Directive = {
    mounted(el, binding, vNode) {
        const {value} = binding;
        const super_admin = "admin";
        const roles = store.getters && store.getters.roles;
        if (value && value instanceof Array && value.length > 0) {
            const roleFlag = value;
            const hasRole = roles.some((role: any) => {
                return super_admin === role || roleFlag.includes(role)
            })
            if (!hasRole) {
                el.parentNode && el.parentNode.removeChild(el)
            }
        } else {
            throw new Error("请设置角色权限标签值")
        }
    }
}