Home > database >  Is it safe to call getters in actions in Vuex on Vue 2?
Is it safe to call getters in actions in Vuex on Vue 2?

Time:03-22

Can somebody explain me this use case in Vue 2?

I have two actions. The second one is called immediately after the first one. The first one set some state variables nad the second one calls getters which are based on that state variables which were set up by first action. I am not sure if the second action will have fresh data from that getters.

Hope someone understand what I mean.

Thanks for help.

CodePudding user response:

If by "action" you mean "dispatch", then no. An "action" is asynchronous, so you will have two "actions" that will attack the same resource without guaranteeing the relevance of the data.

If your first action only modifies the state, then you should use a commit which is synchronous.

Otherwise, you have to wait for the result of your first action. An action always returns a promise so you can do something like this :

dispatch('firstAction', prop)
    .then(function () {
      dispatch('scdAction', prop)
    })
  • Related