-
-
8000
Notifications
You must be signed in to change notification settings - Fork 8.6k
custom directives on components: Can't access child component in directive hooks. #2562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
the vnode represents the element, not the component. The corrent way to access the component ìn Vue 3 is |
I want get CompB component instance in directive, but |
try |
getCurrentInstance is return null, not CompB instance |
I'll reopen this for now to explore this. This is a bit of an edge case where in Vue 2,
The two are usually the same except when using a directive on a child component. I wouldn't not consider this a bug as it is working as expected. We can either find a way to support the edge case or document this as a breaking change. |
I think use |
Is there any further news on this issue? |
I found binding.instance work normal in development, but it turned out to be VNode in production. I use vite for building. |
Finally solved? please |
I'm looking for this to resolve as well.
That's exactly exception I ran into. |
I am now facing the exact same situation that needs to access the child component's context (or called instance) in custom directives. I found that for the mounted arguments ($el, $binding, $vnode, $prevVnode), $vnode.dirs[0].instance sometimes return the child component's context (or instance) and I can manipulate it but not always.... Back to the question itself, I am pretty sure that the child component's context (or instance) is needed to be included in custom directives in some of the use cases and that would be great if the Vue development team could consider adding it back in Vue 3. I'm still looking forward to a solution/workaround. Cheers! Vue Version: 3.2.33 |
Really? What situations would you need the template component instance as opposed to the component instance the directive is applied to? This is my use case: const Autoselect = {
mounted(el, binding, vnode) {
// If the directive is applied to a component and that component has
// an exposed select method, call that, otherwise call select on the DOM element
if (typeof vnode.componentInstance?.select === 'function') {
vnode.componentInstance.select();
} else {
el.select();
}
},
}; Some components have special logic for selecting its text contents. Is there no way to do this? |
I figured out I can do |
I'm also exploring similar things, but unfortunately,
For me, I need to call the component to define functions through |
I have a similar issue, however, while this works in a dev environment we don't get the exposed functions in a build |
I'm sorry that I gave up using custom directives. I believe this is not the functionality I need. The desired feature should be more similar to HOC in React. I just hope that whenever data is passed to the child component, I have the ability to intercept and modify it. |
Version
3.0.2
Reproduction link
https://codesandbox.io/s/weathered-resonance-d2f6t?file=/src/App.vue
Steps to reproduce
What is expected?
vnode.component should be the CompB vue instance
What is actually happening?
vnode.component is null
The text was updated successfully, but these errors were encountered: