3
Hi, I’m trying to call a method that’s in the child component through the parent component. The HOWEVER is that if I involve the child component specifically in a 'v-dialog' the code does not work, but if I put any other element as a 'div' involving the child, it works smoothly.
Here’s the way it works:
<v-btn @click="teste">CHAMAR METODO DO FILHO</v-btn>
<div>
<PageTitle ref="form" />
</div>
And here’s the one that doesn’t work:
<v-btn @click="teste">CHAMAR METODO DO FILHO</v-btn>
<v-dialog>
<PageTitle ref="form" />
</v-dialog>
The exception generated is
Error in v-on handler: "TypeError: Cannot read property 'submit' of undefined"
In the parent component I have this:
methods: {
submit() {
this.$refs.form.submit()
}
},
In the child component I have it:
methods: {
submit() {
...
}
}
If anyone can help I’d appreciate it :D
SOLUTION
The problem is that the property this.$refs
can only be accessed after rendering the component (friend tip James A), then I put my method to run on the property update()
that executes something whenever the component is rendered.
More about the update()
That method
submit
was it you who implemented it? If so, edit the question and include the JS code.– TiagoA
Thanks buddy, I just added :D
– Fernando Lima
In my test worked, want to complement your code? It may be that the problem is in another part of the code.
– TiagoA
With a dialog involving the child component?
– Fernando Lima
I put some component, not using vuetify.
– TiagoA
I did the test with the dialog and it doesn’t really work. The problem is this: in the Vue.js documentation it says that the content of
this.$refs
is populated after rendering, that is, if the element with the reference is not rendered, it will not be accessible via$refs
.– TiagoA
Tiago, thanks so much for the help. I did it! I did the following, since he can only access the
$ref
after rendering the component, I put my method in theupdate()
so every time it renders it runs. Thank you truly :D– Fernando Lima