How to Two-way Data Binding Between Parents and grandchildren in Vue.js How to Two-way Data Binding Between Parents and grandchildren in Vue.js vue.js vue.js

How to Two-way Data Binding Between Parents and grandchildren in Vue.js


There are several ways of doing it, and some are mentioned in other answers:

  1. Use props on components
  2. Use v-model attribute
  3. Use the sync modifier
  4. Use Vuex

For two-way bindings keep in mind that it can cause a chain of mutations that are difficult to maintain,quoted from the docs:

Unfortunately, true two-way binding can create maintenance issues, because child components can mutate the parent without the source of that mutation being obvious in both the parent and the child.

Here are some details to the methods that are available:

1.) Use props on components

Props are easy to use and are the ideal way to solve most common problems.
Because of how Vue observes changes all properties need to be available on an object or they will not be reactive.If any properties are added after Vue has finished making them observable 'set' will have to be used.

 //Normal usage Vue.set(aVariable, 'aNewProp', 42); //This is how to use it in Nuxt this.$set(this.historyEntry, 'date', new Date());

The object will be reactive for both component and the parent:

I you pass an object/array as a prop, it's two-way syncing automatically - change data in the child, it is changed in the parent.

If you pass simple values (strings, numbers) via props, you have to explicitly use the .sync modifier

As quoted from --> https://stackoverflow.com/a/35723888/1087372

2.) Use v-model attribute

The v-model attribute is syntactic sugar that enables easy two-way binding between parent and child. It does the same thing as the sync modifier does only it uses a specific prop and a specific event for the binding

This:

 <input v-model="searchText">

is the same as this:

 <input   v-bind:value="searchText"   v-on:input="searchText = $event.target.value" >

Where the prop must be value and the event must be input

3.) Use the sync modifier

The sync modifier is also syntactic sugar and does the same as v-model, just that the prop and event names are set by whatever is being used.

In the parent it can be used as follows:

 <text-document v-bind:title.sync="doc.title"></text-document>

From the child an event can be emitted to notify the parent of any changes:

 this.$emit('update:title', newTitle)

4.) Use Vuex

Vuex is a data store that is accessible from every component.Changes can be subscribed to.

By using the Vuex store it is easier to see the flow of data mutations and they are explicitly defined. By using the vue developer tools it is easy to debug and rollback changes that were made.

This approach needs a bit more boilerplate, but if used throughout a project it becomes a much cleaner way to define how changes are made and from where.

See the getting started guide


i found this one to be more accurate. https://vuejs.org/v2/guide/components.html#sync-Modifieronly in 2.3.0+ tho. and honestly it's still not good enough. should simply be a easy option for 'two-way' data binding. so none of these options is good.

try using vuex instead. they have more options for such purpose.https://vuex.vuejs.org/en/state.html