How to call function on child component on parent events

后端 未结 9 993
陌清茗
陌清茗 2020-11-27 09:16

Context

In Vue 2.0 the documentation and others clearly indicate that communication from parent to child happens via props.

Question

How does a p

相关标签:
9条回答
  • 2020-11-27 09:55

    I think we should to have a consideration about the necessity of parent to use the child’s methods.In fact,parents needn’t to concern the method of child,but can treat the child component as a FSA(finite state machine).Parents component to control the state of child component.So the solution to watch the status change or just use the compute function is enough

    0 讨论(0)
  • 2020-11-27 10:02

    Give the child component a ref and use $refs to call a method on the child component directly.

    html:

    <div id="app">
      <child-component ref="childComponent"></child-component>
      <button @click="click">Click</button>  
    </div>
    

    javascript:

    var ChildComponent = {
      template: '<div>{{value}}</div>',
      data: function () {
        return {
          value: 0
        };
      },
      methods: {
        setValue: function(value) {
            this.value = value;
        }
      }
    }
    
    new Vue({
      el: '#app',
      components: {
        'child-component': ChildComponent
      },
      methods: {
        click: function() {
            this.$refs.childComponent.setValue(2.0);
        }
      }
    })
    

    For more info, see Vue documentation on refs.

    0 讨论(0)
  • 2020-11-27 10:06

    The below example is self explainatory. where refs and events can be used to call function from and to parent and child.

    // PARENT
    <template>
      <parent>
        <child
          @onChange="childCallBack"
          ref="childRef"
          :data="moduleData"
        />
        <button @click="callChild">Call Method in child</button>
      </parent>
    </template>
    
    <script>
    export default {
      methods: {
        callChild() {
          this.$refs.childRef.childMethod('Hi from parent');
        },
        childCallBack(message) {
          console.log('message from child', message);
        }
      }
    };
    </script>
    
    // CHILD
    <template>
      <child>
        <button @click="callParent">Call Parent</button>
      </child>
    </template>
    
    <script>
    export default {
      methods: {
        callParent() {
          this.$emit('onChange', 'hi from child');
        },
        childMethod(message) {
          console.log('message from parent', message);
        }
      }
    }
    </script>
    
    0 讨论(0)
  • 2020-11-27 10:07

    You can use $emit and $on. Using @RoyJ code:

    html:

    <div id="app">
      <my-component></my-component>
      <button @click="click">Click</button>  
    </div>
    

    javascript:

    var Child = {
      template: '<div>{{value}}</div>',
      data: function () {
        return {
          value: 0
        };
      },
      methods: {
        setValue: function(value) {
            this.value = value;
        }
      },
      created: function() {
        this.$parent.$on('update', this.setValue);
      }
    }
    
    new Vue({
      el: '#app',
      components: {
        'my-component': Child
      },
      methods: {
        click: function() {
            this.$emit('update', 7);
        }
      }
    })
    

    Running example: https://jsfiddle.net/rjurado/m2spy60r/1/

    0 讨论(0)
  • 2020-11-27 10:09

    A simple decoupled way to call methods on child components is by emitting a handler from the child and then invoking it from parent.

    var Child = {
      template: '<div>{{value}}</div>',
      data: function () {
        return {
          value: 0
        };
      },
      methods: {
      	setValue(value) {
        	this.value = value;
        }
      },
      created() {
        this.$emit('handler', this.setValue);
      }
    }
    
    new Vue({
      el: '#app',
      components: {
        'my-component': Child
      },
      methods: {
      	setValueHandler(fn) {
        	this.setter = fn
        },
        click() {
        	this.setter(70)
        }
      }
    })
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
    
    <div id="app">
      <my-component @handler="setValueHandler"></my-component>
      <button @click="click">Click</button>  
    </div>

    The parent keeps track of the child handler functions and calls whenever necessary.

    0 讨论(0)
  • 2020-11-27 10:10

    If you have time, use Vuex store for watching variables (aka state) or trigger (aka dispatch) an action directly.

    0 讨论(0)
提交回复
热议问题