繁体   English   中英

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

[英]Add custom property to vuejs component

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

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

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

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

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

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

看来您应该使用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