31
votes

Fiddle : here

I am creating a webapp with Vue 2 with Vuex. I have a store, where I want to fetch state data from a getter, What I want is if getter finds out data is not yet populated, it calls dispatch and fetches the data.

Following is my Vuex store:

const state = {
  pets: []
};

const mutations = {
  SET_PETS (state, response) {
    state.pets = response;
  }
};

const actions = {
 FETCH_PETS: (state) => {
      setTimeout(function() { 
            state.commit('SET_PETS', ['t7m12qbvb/apple_9', '6pat9znxz/1448127928_kiwi'])
    }, 1000)
 }
}

const getters = {
    pets(state){
    if(!state.pets.length){
        state.dispatch("FETCH_PETS")
    }
    return state.pets
  }
}

const store = new Vuex.Store({
  state,
  mutations,
  actions,
  getters
});

But I am getting following error:

Uncaught TypeError: state.dispatch is not a function(…)

I know I can do this, from beforeMount of Vue component, but I have multiple components which uses same Vuex store, so I have to do it in one of the components, which one should that be and how will it impact other components.

3
I'm looking for an answer to this as well. I understand what GuyC means, but the data I'm hoping to return is a large download that I don't want to trigger when the app first loads. Multiple components need it, but the user could access any of them first (or none of them). - skagzilla

3 Answers

18
votes

Getters can not call dispatch as they are passed the state not context of the store

Actions can call state, dispatch, commit as they are passed the context.

Getters are used to manage a 'derived state'.

If you instead set up the pets state on the components that require it then you would just call FETCH_PETS from the root of your app and remove the need for the getter

8
votes

I know this is an older post and I'm not sure if this is good practice, but I did the following to dispatch from a getter in my store module:

import store from "../index"

And used the store inside my getter like this:

store.dispatch("moduleName/actionName")

I did this to make sure data was made available if it was not already present.

5
votes

had the same Problem.. also wanted all Vue-Instances to automaticly load something, and wrote a mixin:

store.registerModule('session', {
    namespaced: true,
    state: {
        session: {hasPermission:{}},
        sessionLoaded:false
    },
    mutations: {
        changeSession: function (state, value)
        {
            state.session = value;
        },
        changeSessionLoaded: function (state)
        {
            state.sessionLoaded = true;
        }

    },
    actions: {
        loadSession(context)
        {
            // your Ajax-request, that will set context.state.session=something
        }
    }
}); 

Vue.mixin({
    computed: {
        $session: function () { return this.$store.state.session.session; },
    },
    mounted:function()
    {
        if(this.$parent==undefined && !this.$store.state.session.sessionLoaded)
        {
            this.$store.dispatch("session/loadSession");
            this.$store.commit("changeSessionLoaded");
        }
    },
});

because it loads only one per vue-instance and store and it it inlcuded automaticly in every vue-instance, there is no need to define it in every main-app