Accessing one static module from another, without circular imports?
See original GitHub issueI’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:
- Created 4 years ago
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
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: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?