vue.directive(‘hasRole’,{
bind:(el,binding)=>{},
inserted:function(el,binding){
let roles=store.state.userInfo.roles;//roles=[‘admin’,‘user’]
if(!roles.includes(binding.value)){
el.parentNode.removeChild(el)
}
},
})
使用
<button v-hasRole="'admin'">编辑</button>
<button v-hasRole="'admin'">删除</button>
<button >查看</button>