3

Let's say we have a store with two modules, A and B.

All I need is: Access a state property of A while initializing the state of B.

Accessing that property can be done with 'rootState' if the case was trying to access the property inside a getter, action or mutation. But how can I do that while initializing the state?

Example:

A.js

export default  {
   state:{
      x:5
   }
}

B.js

export default  {
   state:{
      foo: 3+ (how to access A.x here?) // this should be 8
   }
}

index.js would look something like this

import moduleA from './modules/A'
import moduleB from './modules/B'

new Vuex.Store({
    modules: {
        A: moduleA,
        B: moduleB,
    },
})
3
  • Hey mate, take a look here pls Commented Oct 6, 2021 at 10:39
  • 1
    @NikolaPavicevic I've visited this link before I posted the question but it doesn't help in my use case. It helps when you need to reference other module state in a modules (action-getter or mutation). Because Vuex gives the option of using rootState as a parameter in all these. What I do need is use that other module state in a module state initialization. Please review the example I supplied. Thank you. Commented Oct 6, 2021 at 11:45
  • have u ever solved this I'm facing the same problem! Commented Jan 2, 2024 at 13:50

0

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.