I see a lot of Vue.js projects using this structure:
├── main.js
├── api
│ └── index.js
│ └── services #containing files with api-calls
│ ├── global.js
│ ├── cart.js
│ └── messages.js
├── components
│ ├── Home.vue
│ ├── Cart.vue
│ ├── Messages.vue
│ └── ...
└── store
├── store.js
├── actions.js #actions to update vuex stores
├── types.js
└── modules
├── global.js
├── cart.js
└── ...
(An example with this structure is 'Jackblog'.)
So, for example, Cart.vue
wants to update the inCart
data in Vuex. To do that, the Cart imports actions.js
:
import { inCart } from '../../store/actions'
The actions.js
imports the api's index.js
so it can connect to the api. And then it updates the values in the Vuex store.
Ok, so that is clear for me. But now, I want to work on the Messages.vue
module. This module should connect to the api as well to get all the messages, but it is not necessary to store the results in Vuex. The only component that needs the data is Messages.vue itself, so it should be stored in the component's data()
only.
Question: I can't import actions.js
inside Messages.vue
because the action shouldn't update Vuex. But I can't move the actions.js
to the api
directory, because that breaks the logic of putting all files that add data to the store in the store-directory. Besides that, the logic should be placed inside Messages.vue
. For example when the api returns an error, a local error
-constant should be set. So it cannot be handled by a separate file.
What is the recommended application structure for making api calls and store them in vuex or local data()
? Where to place the actions file, the api files, and so on? When looking to the Jackblog example it supports Vuex data only. How to restructure this to support both?