[Vuejs]-Vuex access states in actions weird

3👍

It’s because actions do not receive state as a parameter, but context. You need it to dispatch other actions or to commit.

actions: {
  async myAction ({ state, dispatch, commit }) {
    console.log(state.loading);
    await dispatch('anotherAction')
    commit('someCommit')
  }
}

Full list of the context properties here.

2👍

The first argument to an action function is not state, rather context (see docs).

This "context" object has various properties, one of which is state.

This differs from a mutation where the first argument is state.

0👍

refined and working thanks to @FitzFish

import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    loading: false,
    fileStr: "data.json",
  },
  actions: {
    storeTest(context) {
     //works
      console.log("state: ", context.state.fileStr);
    },
  },
});
👤Fred

Leave a comment