3

I need to call a child function from the parent but I don't know how,

This is parent component calling to child component

<QuestionC @childfunction></QuestionC>

This is a child component

methods: {
  childfunction: function () {
    alert('hello')
  }
},
1
  • You can access all child components by this.$root.$children, but I think it's better to using ref to get the target component that you want. Commented Jul 29, 2019 at 11:33

1 Answer 1

3

You can call it using its ref ... like this:

Vue.component('child', {
  template: ' ',
  methods: {
    childfunction: function() {
      alert('hello')
    }
  }
})

new Vue({
  el: '#app',
  methods: {
    callChild() {
      this.$refs.child.childfunction();
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <child ref="child"></child>
  <button @click="callChild()">CallChild</button>
</div>

Sign up to request clarification or add additional context in comments.

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.