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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Whoh! great logic. I appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.