Search code examples
componentsvue.jsvuejs2

VueJs Calling method in Child components


I have a prop

<graph :active-metrics="$data.active_metrics"></graph>

In my child component I can access the value

export default {
      template: '<div>{{activeMetrics}}</div>',
      props: ['active-metrics'],
        methods: {

What I need to do is trigger a method in the child whenever there is a change. How can I achieve this?


Solution

  • You can use v-bind to make the data from the parent flow down to the child.

    In your case it would look something like this:

    <graph v-bind:active-metrics="$data.active_metrics"></graph>
    
    export default {
      template: '<div>{{activeMetrics}}</div>',
      props: ['active-metrics'],
      watch: {
        'active-metrics': function(){
          alert('active-metrics updated');
      }
    }
    

    See here for a working JSFiddle.