R
R
r_g_b_a2020-08-10 12:00:23
Vue.js
r_g_b_a, 2020-08-10 12:00:23

How to return object property in parent component?

Hello.
I have parent component with data like this

data: {
  myObj: {
    first: 'Первый',
    second: 'Второй'
  }
}

and two children, each of which takes its own element of the object
<my-component :obj-val="myObj.first">
  // внутри поле для смены first
<my-component>
<my-component :obj-val="myObj.second">
  // внутри поле для смены second
<my-component>

When the value of first or second changes in the child component , they must be updated in the parent as well.
There were two questions:
1) I can return the value using $emit. But how do you tell which property of an object it is? Instead of returning the value and the key of the object?
2) The purpose of all this is to swap the value in the child components by clicking the button in the parent component. That is, if the value was 1 in the first component, and 2 in the second, then it should become 2 and 1. Therefore, maybe there is an easier option?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-08-10
@r_g_b_a

v-model or sync

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question