D
D
Devero972020-12-17 01:44:10
Vue.js
Devero97, 2020-12-17 01:44:10

How to update data between components?

I have 2 components. The first is tied to creation and updating. That is, when creating, I send the data (all data must be empty), but when I update (I reuse this component), I substitute the data that I received. Just comparison operators. If I have data, then I use it (this is an update), if there is no data, then I use an empty string.
for example

data() {
return {
          title: '' || this.data.title,
          value: null || this.data.tags,
  }
}

Here in the child component, I want to pass either an empty array, or an array with data already.
In parent component, I get data from child when select from list.
Accordingly, I add them to the parent list.
Confusing, of course.
That is, I need, depending on the creation or update, the parent component used either an empty array or an array already filled with data from the child component, but if I want to add more data when updating, then they were reflected when saving in the parent and the next update I want to see already updated list.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2020-12-17
@AlexeyCaTHaR

Store data in a store (Vuex) and get it through mapState where you need it and after filling it, just update it through a mutation?
Then absolutely no difference, child, parent component. You simply control the logic of a particular component based on the data of the store

S
Swert, 2020-12-25
@swert-tech

If you don't need to use Vuex then you can use Props
Pass like this

<template>
<component-first data="{ "key": "data" }"></component-first>
</template>

<<script>
export default {
    components: {
        ComponentFirst
    }
}
</script>

And take it like this
<template>
<p>{{ data }}</p>
</template>

<<script>
export default {
    props: {
        data: Array
    },
    setup (props) {
        // work
    }
}
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question