一、按钮功能权限
1.通过控制按钮显示隐藏来控制功能权限 — vue自定义指令
2.编写自定义指令 v-permission
钩子函数中,接受两个参数:
(1)el 绑定的节点
(2)binding 传递的数据
通过binding.value获取传递的数据
import store from '@/store'function checkPermission (el, binding) {// 获取绑定的值,此处为权限const { value } = binding// 获取所有的功能指令const points = store.getters.userInfo.permission.points// 当传入的指令集为数组时if (value && value instanceof Array) {// 匹配对应的指令const hasPermission = points.some((point) => {return value.includes(point)})// 如果无法匹配,则表示当前用户无该指令,那么删除对应的功能按钮if (!hasPermission) {el.parentNode && el.parentNode.removeChild(el)}} else {// eslint-disabled-next-linethrow new Error('v-permission value is ["admin","editor"]')}}export default {// 在绑定元素的父组件被挂载后调用mounted (el, binding) {checkPermission(el, binding)},// 在包含组件的 VNode 及其子组件的 VNode 更新后调用update (el, binding) {checkPermission(el, binding)}}
