I'm writing a custom directive in vue.
I want it to work like v-if
but it will have a little logic going inside it. Let me explain with an example:
<button v-permission="PermissionFoo">Do Foo</button>
It will check the permission and will show or hide the component.
Currently I'm doing this via CSS styles:
var processPermissionDirective = function (el, binding, vnode) {
if (SOME_LOGIC_HERE) {
el.style.display = el._display;
}
else {
el.style.display = 'none';
}
}
export default {
bind: function (el, binding, vnode) {
el._display = el.style.display;
processPermissionDirective(el, binding, vnode);
},
update: function (el, binding, vnode) {
processPermissionDirective(el, binding, vnode);
}
}
But I don't want this element to stay in the document. So I'm looking for another way other than CSS because it must be also removed from DOM like v-if
does.
Try to use this hack:
UPD 05-19-2017: My latest code. I define
setAttribute()
and check forvnode.componentInstance
to prevent js errors when using with both html elements and Vue components.