question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing one static module from another, without circular imports?

See original GitHub issue

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:

src
├── 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?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

5reactions
garyocommented, Oct 6, 2019

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!

3reactions
garyocommented, Sep 27, 2019

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to avoid circular imports in Python? - Stack Overflow
In some cases, just importing a module with a circular import dependency can result in errors even if you're not referencing anything from...
Read more >
How to Eliminate Circular Dependencies from Your JavaScript ...
Circular dependencies (also known as cyclic dependencies) occur when two or more modules reference each other. // file a. ts import { b...
Read more >
So you got a circular import in Python. | by Hadrien Hamana
You have to know that once fully imported, the module is cached, so any other import statements referencing to the same module will...
Read more >
Python Circular Import Problem and Solutions
Generally, the Python Circular Import problem occurs when you accidentally name your working file the same as the module name and those modules...
Read more >
Avoiding import loops in Python - YouTube
Fix import errors using these tricks.Ever run into an error about a partially initialized module likely due to a circular import ?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found