Calling child method in parent component within a v-dialog

Asked

Viewed 579 times

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.

  • Thanks buddy, I just added :D

  • In my test worked, want to complement your code? It may be that the problem is in another part of the code.

  • With a dialog involving the child component?

  • I put some component, not using vuetify.

  • 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.

  • 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 the update() so every time it renders it runs. Thank you truly :D

Show 2 more comments

1 answer

0


To make it clearer, I’ll add the answer to my comment here:

The problem is this: in the documentation of Vue.js 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.