I created a directive, and I need to make that directive call a function that exists inside the component that has the directive.
// Component with directive, this component has the method 'setDisabled()'
<v-button v-can:edit.disable="[something]" @click="add" />
// Directive created
const can = {
mounted: async (el, binding, vnode) => {
let hasAccess = validateAccess()
if (!hasAccess) {
// should call v-button setDisabled() in here
}
},
}
In VUE 2 that was achievable by using vnode.context, but in VUE 3 it seems like I can only access methods from the parent component with binding.instance.
So, is there a way to use el, binding or vnode, after the component is mounte to call a method? So far just saw the props in vnode, no methods or reactive data.
Official Docs: Script Setup Components using
<script setup>
are closed by default - i.e. the public instance of the component, which is retrieved via template refs or $parent chains, will not expose any of the bindings declared inside .Official Docs: Custom Directives In general, it is not recommended to use custom directives on components.
The binding.instance
argument passed to the directive hook represents
The vnode
argument passed to the directive hook represents
Native DOM Element
Custom component with single root
Custom component with multiple roots
vnode.el.__vueParentComponent.exposed
vnode.el.__vueParentComponent.provides
const setDisabled = () => { /* Your precious method */};
defineExpose({
setDisabled
});
const vCan = {
mounted: async (el, binding, vnode)=> {
vnode.el.__vueParentComponent.exposed.setDisabled()
},
};
vCan