> For the complete documentation index, see [llms.txt](https://devworkshops.gitbook.io/masteringvuejs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devworkshops.gitbook.io/masteringvuejs/state-management/modules.md).

# Modules

In this section, we're going one step further in the **vuex** story and separate areas/domains of the system into modules to make it more maintainable.

First create a **store** folder, moving **store.js** inside, and renaming it to **index.js**. Then, update **main.js** as follows (replace the existing import statement):

{% code title="main.js" %}

```javascript
...
import store from './store/index'
...
```

{% endcode %}

Next, create a new file inside the **store** folder called **supplier.js** with the content below. This file is going to contain anything related to suppliers.

{% code title="supplier.js" %}

```javascript
export default {
    state: {},
    mutations: {},
    actions: {},
    getters: {}
}
```

{% endcode %}

In the **index.js** file, declare the new module as shown below:

{% code title="index.js" %}

```javascript
...
import supplier from './supplier'

export default new Vuex.Store({
    modules: { supplier },
    ...
})

```

{% endcode %}

Next, update the supplier module to include an action to fetch the suppliers and store in the state:

{% code title="supplier.js" %}

```javascript
import { SuppliersService } from '@/services/NorthwindService.js'

export default {
    state: {
        suppliers: []
    },
    mutations: {
        SET_SUPPLIERS(state, payload) {
            state.suppliers = payload
        }
    },
    actions: {
        fetchSuppliers({ commit }) {
            SuppliersService.getAll()
                .then(r => commit('SET_SUPPLIERS', r.data))
                .catch(err => console.error(err))
        }
    }
}
```

{% endcode %}

Now, instead of calling the API directly, dispatch an action to fetch the suppliers:

{% code title="SupplierList.vue" %}

```markup
<template>
  <div>
    ...
    <b-table striped hover :items="$store.state.supplier.suppliers" :fields="fields">
      ...
    </b-table>
  </div>
</template>

<script>
export default {
    data() {
        return {
            fields: ['companyName', 'contactName', 'contactTitle', 'actions']
        }
    },
    created() {
        this.$store.dispatch('fetchSuppliers')
    }
}
</script>
```

{% endcode %}

Now that suppliers are in the store, you can implement some nice features. For example, to only hit the API if the suppliers are not in the state yet. Let's update the **fetchSuppliers** action to include the below:

{% code title="supplier.js" %}

```javascript
...
fetchSuppliers({ state, commit }, force) {
    if (state.suppliers.length > 0 && !force) return
    SuppliersService.getAll()
        .then(r => commit('SET_SUPPLIERS', r.data))
        .catch(err => console.error(err))
}
...
```

{% endcode %}

With this implemented, only a single API request will be made (the first time you navigate to the suppliers page) and following requests will retrieve suppliers directly from the store.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://devworkshops.gitbook.io/masteringvuejs/state-management/modules.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
