K
K
Kiril Kharkevich2019-01-17 13:31:18
Vue.js
Kiril Kharkevich, 2019-01-17 13:31:18

How to pass an array to Vuex store and read this array in another component?

Help guys, I've already broken my whole head. Please give an example for this task:
There is a Menu.vue component and a Card.vue component,
In the Menu.vue component there is a table in which there are rows, by clicking on a row, the data of this row is transferred to the local array selected, for example. How can I get the value of this array on the Card.vue component and subtract it? Everyone says to use the Vuex store (but I've been trying to do it for the 3rd day and nothing is transmitted). Please give an example with an array and a store, how to properly organize a store and transfer an array.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kiril Kharkevich, 2019-01-17
@kirill98402

Look, there is a method for writing data to an array:

onSelect (items) {        
        this.selected = items
        this.$emit('selected',selected) 
      }

Here is the method for getting data from an array:
fetchData () {
        this.menuVisible = !this.menuVisible;
      axios.get('https://db-http.firebaseio.com/devices.json')
     .then(res => {
          console.log(res)
          const data = res.data
          const devices = []
          for (let key in data) {
            const device = data[key]
            device.id = key
            devices.push(device)
          }
         this.$on('selected',function(selected) {
           this.devices = Object.entries(selected).map(([ id, n ]) => ({ ...n, id }));
         })
        })
        .catch(error => console.log(error))
    }

Did I apply right? But it doesn't work like that

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question