fix: the problem that v-auth directive doesn't take effect in native html element; 🐛

修复:v-auth 指令在原生 HTML 元素上不生效的 bug;
This commit is contained in:
iczer 2020-08-06 16:18:54 +08:00
parent 38363f2ec2
commit 4241156903
2 changed files with 23 additions and 17 deletions

View File

@ -42,33 +42,33 @@ const auth = function(authConfig, permission, role, permissions, roles) {
return false
}
/**
* 阻止的 click 事件监听
* @param event
* @returns {boolean}
*/
const preventClick = function (event) {
event.preventDefault()
event.stopPropagation()
return false
}
const checkInject = function (el, binding,vnode) {
const type = binding.arg
const check = binding.value
const instance = vnode.context
const $auth = instance.$auth
if (!$auth || !$auth(check, type)) {
el.classList.add('disabled')
el.setAttribute('title', '无此权限')
el.addEventListener('click', preventClick, true)
addDisabled(el)
} else {
el.classList.remove('disabled')
el.removeAttribute('title')
el.removeEventListener('click', preventClick, true)
removeDisabled(el)
}
}
const addDisabled = function (el) {
if (el.tagName === 'BUTTON') {
el.setAttribute('disabled', 'disabled')
} else {
el.classList.add('disabled')
}
el.setAttribute('title', '无此权限')
}
const removeDisabled = function (el) {
el.classList.remove('disabled')
el.removeAttribute('disabled')
el.removeAttribute('title')
}
const AuthorityPlugin = {
install(Vue) {
Vue.directive('auth', {
@ -77,6 +77,9 @@ const AuthorityPlugin = {
},
update(el, binding,vnode) {
checkInject(el, binding, vnode)
},
unbind(el) {
removeDisabled(el)
}
})
Vue.mixin({

View File

@ -36,6 +36,9 @@ function parseRoutes(routesConfig, routerMap) {
page: routeCfg.page || router.page
}
}
if (routeCfg.invisible || router.invisible) {
route.meta.invisible = true
}
if (routeCfg.children && routeCfg.children.length > 0) {
route.children = parseRoutes(routeCfg.children, routerMap)
}