How use Vue.set() in NuxtJs application?

Viewed 5899

I want to set a property, by using Vue.set() in Nuxtjs application.

editPost(post) {
    Vue.$set(post, 'edit', true)
}

Got error: Vue is not defined

2 Answers

You can just use this instead of Vue.. This references the current vue component inside the method, so it will work same way:

editPost(post) {
    this.$set(post, 'edit', true)
}

Sometimes you may want to assign a number of properties to an existing object, for example using Object.assign() or _.extend(). However, new properties added to the object will not trigger changes. In such cases, create a fresh object with properties from both the original object and the mixin object:

// instead of Object.assign(this.someObject, { a: 1, b: 2 }) this.someObject = Object.assign({}, this.someObject, { a: 1, b: 2 })

Related