VueJs Calling method in Child components

坚强是说给别人听的谎言 提交于 2019-12-22 08:35:31

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/40806014/vuejs-calling-method-in-child-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!