Home > Net >  Accessing state inside action shows empty state
Accessing state inside action shows empty state

Time:10-27

After reloading the page (using vuexPersistedState) I access my state inside an action like:

    updateOuterValue: ({
        commit
    }, data) => {
      console.log(state);
      ...
    }

The localstorage shows the preserved state, also it's delivered to the components as it should. But inside the action the state is empty. How comes it shows empty state values?

CodePudding user response:

Okay, I got it. I can use getters to access the state inside an action:

    updateOuterValue: ({
        commit,
        getters
    }, data) => {
      ...
      console.log(getters.getForm); // WORKS
      ...
    }

Another way doing this is handling the state directly, like:

    updateOuterValue: ({
        commit,
        state       // <--- HERE
    }, data) => { ... }

CodePudding user response:

The first argument of your action method is the context and the second one is the payload :

 updateOuterValue: (context, data) => { 
   //here you could get access to the other options via context

     context.state.outerValue=data;

 }

or destruct the context as you did in your answer

  • Related