2

I am trying to update state variable's value from a vue file and I want that updated value in a javascript file. I am updating value using mutations from vue file but when I retrieve value inside of javascript file it is showing state variable's old value.

code snippets: 1. I am updating a state variable's value from Sample.vue file. 2. That gets updated in mutations 3. I want updated state variable's value in sample.js which is a javascript file. 4. The last code snippet is of state in which I have declared state variable i.e., in state.js file.

// Sample.vue 
this.$store.commit('setStateVariable','newValue');
// mutations.js
setStateVariable(state, payload) {
        state.newValue = payload;
}
// sample.js
import state from "../store/state";
   console.log(state.newValue);
// state.js
export default{
newValue: 'Hello World'
}

Note: Above code snippets are from 4 separate files and included here to just make question more understandable. And there is no import export issue in actual code. In a nutshell, I am not getting updated value of state's variable in javascript file(NOT VUE FILE) even though I have updated it from a vue file. Thanking you in advance.

Aryesh
  • 386
  • 2
  • 16

1 Answers1

1

My idea is to import the store in sample.js file and use it like the following store.state.newValue.

I've created sample here.

Ali
  • 1,055
  • 9
  • 17