2016-04-21 42 views
0

如何在vue組件中設置自定義屬性?將自定義屬性添加到vuejs組件

var myComponent = Vue.extend({ 
    data: function() { 
     return { 
      item: {} 
     } 
    }, 

    created: function() { 
     // This does not seem to work 
     this.item.customProperty = 'customProperty'; 
    } 
}); 

回答

1

您可以使用Vue.set添加反應:

var myComponent = Vue.extend({ 
    data: function() { 
     return { 
      item: {} 
     } 
    }, 

    created: function() { 
     Vue.set(this.item, 'customProperty', 'customProperty'); 
    } 
}); 
+0

謝謝,就是這樣! – user2968356

0

看來,你應該使用Object.assign:

var myComponent = Vue.extend({ 
    data: function() { 
     return { 
      item: {} 
     } 
    }, 

    created: function() { 
     // This does not seem to work 
     this.item = Object.assign(this.item, {customProperty:'customProperty'}); 
    } 
}); 
+0

謝謝!這增加了屬性,但不幸的是不添加反應性的getter和setter方法。有沒有辦法做到這一點? – user2968356

相關問題