How to access to a child method from the parent in vue.js

al3x :

I have two nested components, what is the proper way to access to the child methods from the parent ?

this.$children[0].myMethod() seems to do the trick but it is pretty ugly, isn't it, what can be better way:

<script>
import child from './my-child'

export default {
  components: {
   child
  },
  mounted () {
    this.$children[0].myMethod()
  }
}
</script>
Desmond Lua :

You can use ref.

import ChildForm from './components/ChildForm'

new Vue({
  el: '#app',
  data: {
    item: {}
  },
  template: `
  <div>
     <ChildForm :item="item" ref="form" />
     <button type="submit" @click.prevent="submit">Post</button>
  </div>
  `,
  methods: {
    submit() {
      this.$refs.form.submit()
    }
  },
  components: { ChildForm },
})

If you dislike tight coupling, you can use Event Bus as shown by @Yosvel Quintero. Below is another example of using event bus by passing in the bus as props.

import ChildForm from './components/ChildForm'

new Vue({
  el: '#app',
  data: {
    item: {},
    bus: new Vue(),
  },
  template: `
  <div>
     <ChildForm :item="item" :bus="bus" ref="form" />
     <button type="submit" @click.prevent="submit">Post</button>
  </div>
  `,
  methods: {
    submit() {
      this.bus.$emit('submit')
    }
  },
  components: { ChildForm },
})

Code of component.

<template>
 ...
</template>

<script>
export default {
  name: 'NowForm',
  props: ['item', 'bus'],
  methods: {
    submit() {
        ...
    }
  },
  mounted() {
    this.bus.$on('submit', this.submit)
  },  
}
</script>

https://code.luasoftware.com/tutorials/vuejs/parent-call-child-component-method/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I access a parent method from child component in Vue.js?

Executing child method from parent component in Vue.js

Use method from child component in parent with CDN vue.js

vue js how to pass data from parent, v-for loop list to child component with method

How to access a property or a method of alpine.js parent component from a child component?

How can I access child component data from a parent in Vue?

How to access a child class method from a parent class?

How to access a child's inherited member from a parent's method

How to send data from parent to child component in Vue.js

How to access the parent controller from the child routes in Ember JS?

Firing child method from parent vue 2

Javascript Access to parent properties from child method

Access parent method from a dynamically added child

Access Parent Fragment method from Child Fragment

Access Parent method from child Javascript

Vuejs: Access a Parent Method from Child Component

Passing data from child to parent to parent in Vue.js with method $emit()

Update child component from parent in Vue js

Reactjs: How to access state of child component.from a method in parent component that depends on state of child component

How to access parent class instance method from child class static method in python?

How to access parent class variable in child class inside a child method?

Pass data from parent component to child of child in Vue js

Vue js reach child components child from parent

How to access parent ref from child

How to access a subclass method from parent method?

How to invoke parent private method from child?

How Call child method from parent class

How to Overload a method from parent to child

How to access the init variable obtained from parent class into child method in python?