const store = new Vuex. Store ( {
state : {
permission_buttons : [ 'template:add' ]
} ,
getters : {
permission_buttons : ( state ) => state. permission_buttons
} ,
mutations : {
PERMISSION_BUTTONS : ( state, data ) => {
state. permission_buttons = data
}
}
} )
import Vue from 'vue'
import store from './../store'
Vue. directive ( 'permission' , {
inserted ( el, binding, vnode ) {
const { value } = binding
const all_permission = '*:*:*'
const permissions = store. getters && store. getters. permission_buttons
if ( value && value instanceof Array && value. length > 0 ) {
const permissionFlag = value
const hasPermissions = permissions. some ( ( permission ) => {
return (
all_permission === permission || permissionFlag. includes ( permission)
)
} )
if ( ! hasPermissions) {
el. parentNode && el. parentNode. removeChild ( el)
}
} else {
throw new Error ( ` 请设置操作权限标签值 ` )
}
}
} )
import './config/directive'
< el-button
type = " primary"
@click = " handlerAdd"
v-permission = " [' template:add' ]"
> 新增</ el-button
>