2016-11-18 42 views
5

嘿,我很新Vue.js,我試圖完成似乎是一件簡單的事情,但我有麻煩。基本上,我需要它,所以每當組件加載到DOM中時,其中一個方法就會觸發。這是我目前的代碼,我試圖使用v-on:load,但似乎不起作用。在加載vue.js時運行組件方法

Vue.component('graph', { 
 
    props:['graphId','graphData'], 
 
    template: '<canvas v-on:load="{{populateGraph()}}"></canvas>', 
 
    methods: { 
 
     initGraph: function() { 
 
      var settlementBalanceBarChart = new Chart(this.graphId, { 
 
       type: "bar", 
 
       data: settlementBalanceBarData, 
 
       options: settlementBalanceBarOptions 
 
      }); 
 
     }, 
 
     //this is the function I would like to run 
 
     populateGraph: function() { 
 
      alert('{{graphId}}'); 
 
     } 
 
    } 
 

 
}); 
 

 
var vm = new Vue({ 
 
    el: "#app", 
 
    mounted: function(){ 
 

 
    } 
 

 
});

罰款相同的代碼的功能,如果我使用V-上:單擊事件

回答

7

instance lifecycle hooks,您可以使用該。例如:

Vue.component('graph', { 
    props:['graphId','graphData'], 
    template: '<canvas></canvas>', 
    created: function() { 
     alert('{{graphId}}'); 
    }, 
    methods: {} 
}); 
+0

真棒,這工作:)謝謝! –