How to call function on child component on parent events

后端 未结 9 994
陌清茗
陌清茗 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 10:13

    Did not like the event-bus approach using $on bindings in the child during create. Why? Subsequent create calls (I'm using vue-router) bind the message handler more than once--leading to multiple responses per message.

    The orthodox solution of passing props down from parent to child and putting a property watcher in the child worked a little better. Only problem being that the child can only act on a value transition. Passing the same message multiple times needs some kind of bookkeeping to force a transition so the child can pick up the change.

    I've found that if I wrap the message in an array, it will always trigger the child watcher--even if the value remains the same.

    Parent:

    {
       data: function() {
          msgChild: null,
       },
       methods: {
          mMessageDoIt: function() {
             this.msgChild = ['doIt'];
          }
       }   
       ...
    }
    

    Child:

    {
       props: ['msgChild'],
       watch: {
          'msgChild': function(arMsg) {
             console.log(arMsg[0]);
          }
       }
    }
    

    HTML:

    <parent>
       <child v-bind="{ 'msgChild': msgChild }"></child>
    </parent>
    
    0 讨论(0)
  • 2020-11-27 10:14

    What you are describing is a change of state in the parent. You pass that to the child via a prop. As you suggested, you would watch that prop. When the child takes action, it notifies the parent via an emit, and the parent might then change the state again.

    var Child = {
      template: '<div>{{counter}}</div>',
      props: ['canI'],
      data: function () {
        return {
          counter: 0
        };
      },
      watch: {
        canI: function () {
          if (this.canI) {
            ++this.counter;
            this.$emit('increment');
          }
        }
      }
    }
    new Vue({
      el: '#app',
      components: {
        'my-component': Child
      },
      data: {
        childState: false
      },
      methods: {
        permitChild: function () {
          this.childState = true;
        },
        lockChild: function () {
          this.childState = false;
        }
      }
    })
    <script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.2.1/vue.js"></script>
    <div id="app">
    <my-component :can-I="childState" v-on:increment="lockChild"></my-component>
    <button @click="permitChild">Go</button>
    </div>

    If you truly want to pass events to a child, you can do that by creating a bus (which is just a Vue instance) and passing it to the child as a prop.

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

    you can use key to reload child component using key

    <component :is="child1" :filter="filter" :key="componentKey"></component>
    

    If you want to reload component with new filter, if button click filter the child component

    reloadData() {            
       this.filter = ['filter1','filter2']
       this.componentKey += 1;  
    },
    

    and use the filter to trigger the function

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