K
K
Kiril Kharkevich2019-01-16 14:01:39
Vue.js
Kiril Kharkevich, 2019-01-16 14:01:39

What is the correct way to write data to an array in the Vuex store and get data from this array in another component?

Good day! There is such a task: I have two components (1st table with fields on which I click and enter data into an array - but so far it only works locally), (2nd component on which I need to display data from this array in cards) . It was decided that the Vuex store should be used. That's what I got to but, the data is not written to the array in the store. I ask for help with a solution or an example of my problem, I searched through a bunch of topics, but I could not figure it out - I have been racking my brains with this problem for the 3rd day.
Here is the action by which data is written to the local array:

<md-table v-model="devices" md-card @md-selected="onSelect">
      <md-table-row slot="md-table-row" slot-scope="{ item }" :md-disabled="item.named.includes('Stave')" md-selectable="multiple" md-auto-select>
        <md-table-cell md-label="Выбрать все устройства">{{ item.named }}</md-table-cell>
      </md-table-row>
    </md-table>

Here is a method that writes data to the local array and store the note array (most likely not true):
onSelect (items) {       
        this.selected = items
        this.note = this.selected
        сonsole.log(note)
      }

Here is my Vuex store:
const store = new Vuex.Store({
    state: {
        notes: []
    },
    actions: {
        addNote({commit}, note) {
            commit('ADD_NOTE', note)
        }
    },
    mutations: {
        ADD_NOTE(state, note) {
            state.notes.push(note)
        }
    },
    getters: {
        notes(state) {
            return state.notes
        }
    },  
    modules: {
    
    }
})

new Vue ({
    el: '#checkboxmenu',
    store,
    computed: {
        notes() {
            return this.$store.getters.notes;
        }
    },
    methods: {
        addNew() {
            this.$store.dispatch('onSelect')
        }
    }
})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question