// 以插件的方式用全局指令实现按钮权限
export default {
install(app) {
app.directive('has', {
// vue3全局指令钩子函数 组件及子组件更新完成后调用
updated(el, binding) {
dealPermission(el, binding)
},
// vue2全局指令钩子函数 组件及子组件更新完成后调用
componentUpdated(el, binging) {
dealPermission(el, binging)
}
})
}
}
// permissionsList为权限列表数组
const permissionsList = [{permission: 'HOME'}]
const dealPermission = (el, binding) => {
const permission = binding.value
if (!permission) {
return
}
let hasPermission = false
for (let i = 0; i < permissionsList.length; i++) {
if (permissionsList[i].permission === permission) {
hasPermission = true
break
}
}
if (!hasPermission) {
el.style.display = 'none'
}
}
08-12
4675
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)