permission.js
const roleList = ['/list', '/detail']
function checkPermission(el, binding, vnode) {
const { value } = binding
if (value) {
const hasPms = codeList.includes(value)
if(!(hasPms)){
const comment = document.createComment(' ')
Object.defineProperty(comment, 'setAttribute', { value: () => undefined })
vnode.elm = comment;
vnode.text = ' ';
vnode.isComment = true;
vnode.context = undefined;
vnode.tag = undefined;
vnode.data.directives = undefined;
if (vnode.componentInstance) {
vnode.componentInstance.$el = comment;
}
if (el.parentNode) {
el.parentNode.replaceChild(comment, el);
}
}
} else {
throw new Error(`v-permission="/list"`)
}
}
export default {
inserted(el, binding, vnode) {
checkPermission(el, binding, vnode)
},
update(el, binding, vnode) {
checkPermission(el, binding, vnode)
}
}
index.js
import permission from './permission'
const install = function(Vue) {
Vue.directive('permission', permission)
}
if (window.Vue) {
window['permission'] = permission
Vue.use(install);
}
permission.install = install
export default permission
main.js-全局自定义指令
import permission from '@/directive/permission/index.js'
Vue.use(permission)
temp.vue
<template>
<span v-permission="/list">当前已登录的人员是否有该权限显示</span>
</template>