6

I just want to know how to call a parent function from a child component.

I've tried to use the $parent to call the parent method, but I get this error

TypeError: _this.$parent.forceRender is not a function

Here's the parent method that I'm trying to call:

methods: {
      forceRender() {
        this.componentKey += 1
      }
    },

And here's the child component as you can see I'm trying to call the parent method using the $parent:

this.$parent.forceRender()
1
  • By the way: forceRerender sounds like it's a bad idea. You usually should not have to do something like that. Commented Nov 13, 2020 at 22:09

3 Answers 3

13

You can send the functions as a props to the child component jus like follows

<child-component :forceRender="forceRender" />

inside the child component you can received it like this

props: ['forceRender']

and then call it as

this.forceRender()
Sign up to request clarification or add additional context in comments.

Comments

4

You should emit an event from child to parent component in order to run a parent method :

in child component:

this.$emit('force-render')

in parent component add @force-render to component tag with forceRender as handler :

<child-component @force-render="forceRender" />

Comments

0

I'm not sure but I think you came from React Like one way data binding library, but here in Vuejs you have two way data communication, so instead of doing these work around you can directly tell the parent components by emitting the event and listen the event in parent and react based on your requirements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.