简体   繁体   中英

Add custom property to vuejs component

How do I set a custom property inside a vue component?

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

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

You can use Vue.set to add reactivity:

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

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

It seems that you should use 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'});
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM