Vue: Merging data reactively

Created on 18 May 2016  路  1Comment  路  Source: vuejs/vue

There seems to be no way to use Vue's merge strategies when assigning new data to a mounted instance. Mixins and vm.$options = Vue.util.mergeOptions(vm.$options, {data: x => newData}, vm) aren't reactive.

Something like vm.$merge(newData) or vm.$mixin({data: newData}) would be nice. The ability to add mixins on the fly would make Vue very flexible (vm.$mixin({computed: {x: newFn}}) would be a neat way to reactively change computed functions, for example), but $merge is probably much easier to implement.

Most helpful comment

I don't like the idea of runtime mixins, because it makes your components' behavior unpredictable depending on when mixins are applied.

Merging data and merging options are totally different concepts. Data is per-instance state, options is like the class definition of your component.

As for merging data, it is recommended to just replace the original object with the new data:

vm.$data = Object.assign({}, vm.$data, newData)

>All comments

I don't like the idea of runtime mixins, because it makes your components' behavior unpredictable depending on when mixins are applied.

Merging data and merging options are totally different concepts. Data is per-instance state, options is like the class definition of your component.

As for merging data, it is recommended to just replace the original object with the new data:

vm.$data = Object.assign({}, vm.$data, newData)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

gkiely picture gkiely  路  3Comments

seemsindie picture seemsindie  路  3Comments

bfis picture bfis  路  3Comments

wufeng87 picture wufeng87  路  3Comments

robertleeplummerjr picture robertleeplummerjr  路  3Comments