V
V
Vladimir Golub2020-01-24 13:00:50
Vue.js
Vladimir Golub, 2020-01-24 13:00:50

getter not called after mutation?

After going to the page and pressing the button, the getter called on the page through the computed property does not work.
Although after reloading the page everything works. What could be the problem ?
I put the console after the mutation, the data writes new ones, since the language is switched.

export const state = () => {
  items: []
};

export const getters = {
  getMenuItems: state => state.items
};

export const mutations = {
  setMenu(state, items) {
    state.items = items;
  }
};

export const actions = {
  async getMenu({rootState, commit}) {
    const { auth, lang } = rootState;
    
    const {items} = await this.$axios.$get(`/api/${ lang.locale }/menu`, {
      headers: {
        'Authorization': `bearer ${auth.token}`,
        'Accept-Language': `${lang.locale}`
      },
    });

    if (items) {
      // set items
      commit('setMenu', items);
    }
  }
};
```
```
    computed: {
      ...mapGetters({menuItems: 'menu/getMenuItems'}),
    },
    watch: {
      menuItems: function (newValue, oldValue) {
        console.log(newValue); // это сообщение не выводится в описанном случае
      }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Golub, 2020-01-24
@RazerVG

Found my mistake

export const state = () => {
  items: []
};

I forgot to wrap it in brackets.
export const state = () => ({
  items: []
});

It's strange that it worked after reloading the page.

A
Alex, 2020-01-24
@Kozack Vue.js

Perhaps somewhere they broke the reactivity. Try changing the data via Vue.set

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question