Vuex-module-decorators: Accessing one static module from another, without circular imports?

Created on 27 Sep 2019  ยท  5Comments  ยท  Source: championswimmer/vuex-module-decorators

I'm using static modules (using the method in #178), see test repo at https://github.com/garyo/vuex-problem-test.git.
I have this layout:

โ”œโ”€โ”€ App.vue
โ”œโ”€โ”€ main.ts
โ””โ”€โ”€ store
    โ”œโ”€โ”€ index.ts
    โ”œโ”€โ”€ modules
    โ”‚ย ย  โ”œโ”€โ”€ modB.ts
    โ”‚ย ย  โ””โ”€โ”€ user.ts
    โ””โ”€โ”€ store-accessor.ts

i.e. two modules, a store-accessor which imports those and exports the stores, and store/index.ts which imports store-accessor and exports all the stores.

This is all OK, until modB.ts needs to use some state from user.ts. I add
import { userStore } from '@/store'
to `modB.ts, which creates an import cycle:
store/index.ts -> store/store-accessor.ts -> store/modules/modB.ts -> store/index.ts
Is there any way around that?

Most helpful comment

By the way I renamed my test repo above to https://github.com/garyo/vuex-module-decorators-example -- since it's an example, not a problem anymore!

All 5 comments

If you _only_ need to access the other module's state (not getters or actions or mutations), this works:
In the module modB that needs to use the other module, do this:

import userModule from '@/store/modules/user'
...
get usingUserMod() {
    const userStore = this.context.rootState.user as userModule
    const uid = userStore.uid
   // now use uid as usual
}

It works because it uses the dynamic context in the method rather than trying to get the root store when the module is loaded.

But that cast isn't really correct; userStore is not really a userModule. So if you try to call any of its methods or use any of its getters, they will fail at runtime ("userStore.setUid is not a function").

Any hints?

After doing a bunch of reading of various things, I now understand that circular imports don't always have to be avoided -- if they're managed carefully they can work fine because of the two-phase import logic ( Instantiation then Evaluation).

I updated the above repo, https://github.com/garyo/vuex-problem-test.git, with a full working example of static vuex-module-decorator modules using the the method in #178 and calling each other via properly managed circular imports.

I now know enough to help write a section of the doc for how to do this, if folks are interested. In fact I'd suggest starting with the README from my test repo: https://github.com/garyo/vuex-problem-test/blob/master/README.md

By the way I renamed my test repo above to https://github.com/garyo/vuex-module-decorators-example -- since it's an example, not a problem anymore!

Thaks @garyo, you saved my day. I'm quite new in typescript, but shouldn't static modules with communication with each other work out of the box? It seems not a problem in plain vuex.

This should be a feature of this package.

However, Vue 3 being close to release in a couple of months, it will be interesting to see how Vuex is changing on or after the release...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidhiendl picture davidhiendl  ยท  3Comments

Leandro-Albano picture Leandro-Albano  ยท  3Comments

Akumzy picture Akumzy  ยท  5Comments

championswimmer picture championswimmer  ยท  6Comments

stevefan1999-personal picture stevefan1999-personal  ยท  5Comments