Update data property / object in vue.js

后端 未结 1 1944
無奈伤痛
無奈伤痛 2021-01-12 07:14

is there a way I can programmatically update the data object / property in vue.js? For example, when my component loads, my data object is:

data         


        
相关标签:
1条回答
  • 2021-01-12 07:48

    Vue does not allow dynamically adding new root-level reactive properties to an already created instance. However, it’s possible to add reactive properties to a nested object, So you may create an object and add a new property like that:

    data: function () {
        return {
            someObject:{
                cars: true,
        }
    }
    

    and add the property with the set method:

    methods: {
            click_me: function () {
                this.$set(this.someObject, 'planes', true)
            }
        }
    

    for vue 1.x use Vue.set(this.someObject, 'planes', true)

    reactivity

    0 讨论(0)
提交回复
热议问题