Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is computed value not updated after vuex store update?

I got a printerList computed property that should be re-evaluated after getPrinters() resolve, but it look like it's not.

sources are online: optbox.component.vue, vuex, optboxes.service.js

Component

<template>     <div v-for="printer in printersList">         <printer :printer="printer" :optbox="optbox"></printer>     </div> </template> <script> … created() { this.getPrinters(this.optbox.id); },     computed: {         printersList() {             var index = optboxesService.getIndex(this.optboxesList, this.optbox.id);             return this.optboxesList[index].printers         }     },     vuex: {         actions: { getPrinters: actions.getPrinters,},         getters: { optboxesList: getters.retrieveOptboxes}     } <script> 

Actions

getPrinters({dispatch}, optboxId) {     printers.get({optbox_id: optboxId}).then(response => {         dispatch('setPrinters', response.data.optbox, response.data.output.channels);     }).catch((err) => {         console.error(err);         logging.error(this.$t('printers.get.failed'))     }); }, 

Mutations

setPrinters(optboxes, optboxId, printers) {     var index = this.getIndex(optboxes, optboxId);     optboxes[index] = {...optboxes[index], printers: printers } }, 

Question

Why does the printerList computed property isn't re-evaluated (i.e. the v-for is empty)

like image 860
Édouard Lopez Avatar asked Aug 07 '16 22:08

Édouard Lopez


People also ask

How do I update my Vuex data?

To update existing data, you can do so with the update method. Following example is some simple Vue Component handling data update with form data. The update method takes where condition and data as payload. where condition can be a Number or a String representing the value of the primary key of the Model.

Does Vuex keep state on refresh?

However, when the page is refreshed, the data in vuex will be reinitialized, resulting in data loss. Because the data in vuex is saved in the running memory, when the page is refreshed, the page will reload the vue instance, and the data in vuex will be re assigned.

Are the results of Vuex getter cached?

Vuex allows us to define "getters" in the store. You can think of them as computed properties for stores. As of Vue 3.0, the getter's result is not cached as the computed property does.

Which part of Vuex is responsible for directly making changes to the data store?

The second key defined in the store is the mutations key. As the name suggests, it contain an object of all of the properties responsible for making changes to the state .


2 Answers

It is due to this line: optboxes[index] = {...optboxes[index], printers: printers }.

You are directly setting item with index, which can't be observed by Vue

Try splicing the old item from array and pushing the new one.

like image 180
bartlomieju Avatar answered Oct 11 '22 03:10

bartlomieju


You could do this:

Vue.set(optboxesList[index], 'printers', printers ) 
like image 28
Gabriel Robert Avatar answered Oct 11 '22 03:10

Gabriel Robert