简体   繁体   English

向vue.js组件添加自定义属性

[英]Add custom property to vuejs component

How do I set a custom property inside a vue component? 如何在vue组件中设置自定义属性?

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: 您可以使用Vue.set添加反应性:

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

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

It seems that you should use Object.assign: 看来您应该使用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'});
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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