将自定义属性添加到vuejs组件

时间:2016-04-21 08:21:33

标签: javascript vue.js

如何在vue组件中设置自定义属性?

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

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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分: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'});
    }
});