Skip to content
Advertisement

Issue with Dependency cycle via in Vue.js

I’m having an issue with a linting error in a vue.js project. The error that I get looks like this:

JavaScript

I have no idea how to get rid of this error. I tried renaming files, using this.$router and this.$store with no luck. Here is some of my code:

router -> index.js: The data path is the main one I want to get to. Notice that I have the store import files commented out – that does get rid of the dependency error but then I have issues with doing something like:

JavaScript

as opposed as importing the store and doing this:

JavaScript
JavaScript

store/modules/common.js:

JavaScript

In the common.js file I’ve tried commenting out:

JavaScript

and that seemed to work – got the Dependency cycle error to go away and in the router/index.js file I was able to get to the route but had an issue with this.$store.state.common.loginFlag when I commented out import store from ‘@/store/index’; If I leave in the import of: import store from ‘@/store/index’; then I get the dependency cycle error.

I’ve also found some help at these other stack pages:

TypeError: Cannot read properties of undefined (reading ‘$router’) vuejs

dependency cycle detected import/no-cycle

I will say that I hate using linters and that’s what’s giving me the problem here.

Here is the code for store/index.js:

JavaScript

Advertisement

Answer

Looks like the reason for the dependency cycle here is when you are importing router setup in the store module, and the router in turn imports the whole store. It’s okay to use store in router, but try to move routing/redirect logic (these lines):

JavaScript

from /modules/common.js to the component or global router hook level, so you avoid router import in the store module.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement