vue3封装一个指令实现权限按钮
模式1:数组模式-可支持多个权限一起控制按钮展示与隐藏
1-1 directives / index.ts
export * from './permission'
1-2 directives / permission.ts
import type {
Directive } from 'vue'
export const permission: Directive = {
mounted(el, binding) {
const {
value } = binding
const permissionBtn:any = sessionStorage.getItem('permission')
if (value && value instanceof Array && value.length > 0) {
const permissionFunc = value
let arr = JSON.parse(permissionBtn)
const hasPermission = arr.some((role: any) => {
return permissionFunc.includes(role)
})
if (!hasPermission) {
el.style.display = 'none'
}
} else {
throw new Error('need roles! Like v-permission="[\'admin\',\'editor\']"')
}
}
}
1-3 main.ts
import {
createApp } from 'vue'
import type {
Directive } from "vue"
import * as directives from<