提交代码
This commit is contained in:
6
src/directives/index.js
Normal file
6
src/directives/index.js
Normal file
@ -0,0 +1,6 @@
|
||||
import permission from "./permission";
|
||||
|
||||
export default app => {
|
||||
app.directive('permission',permission)
|
||||
}
|
||||
|
34
src/directives/permission.js
Normal file
34
src/directives/permission.js
Normal file
@ -0,0 +1,34 @@
|
||||
import store from '@/store'
|
||||
function checkPermission(el,binding) {
|
||||
//获取对应的权限
|
||||
const { value } = binding;
|
||||
|
||||
//获取当前用户的所有功能权限
|
||||
const buttonPermission = store.getters.userInfo?.permission?.buttonPermission;
|
||||
//value必须是一个数组
|
||||
if (value && value instanceof Array && buttonPermission.length > 0) {
|
||||
//匹配对应的指令
|
||||
const hasPermission = buttonPermission.some(item => {
|
||||
return value.includes(item)
|
||||
})
|
||||
|
||||
//匹配失败
|
||||
if (!hasPermission) {
|
||||
el.parentNode && el.parentNode.removeChild(el)
|
||||
}
|
||||
} else {
|
||||
throw new ErrorEvent('v-permisss value must 酱紫【"admin" ,"login"】...')
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
//在绑定 元素的父组件被挂载之后调用
|
||||
mounted (el,binding) {
|
||||
checkPermission(el,binding)
|
||||
},
|
||||
// 在包含组件的VNode 及其子组件的VNode更新后调用
|
||||
update (el,binding) {
|
||||
checkPermission(el,binding)
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user