簡體   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