Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

VueJS - Modules

Structuring Vuex Store Using Modules

As your Vuex store grows, it can become difficult to manage all the state, mutations, actions, and getters in a single store file. Vuex allows you to divide your store into modules, making it easier to manage and scale your application. This guide covers how to structure your Vuex store using modules.

Key Points:

  • Modules allow you to divide your Vuex store into smaller, manageable pieces.
  • Each module can contain its own state, mutations, actions, and getters.
  • Modules can be namespaced to avoid naming conflicts and improve code organization.

Setting Up Modules

Creating a Module

First, create a module by defining its state, mutations, actions, and getters:


// src/store/modules/counter.js
const state = {
  count: 0
};

const mutations = {
  increment(state) {
    state.count++;
  }
};

const actions = {
  increment({ commit }) {
    commit('increment');
  }
};

const getters = {
  count: state => state.count
};

export default {
  state,
  mutations,
  actions,
  getters
};
                

Registering the Module

Next, register the module in your Vuex store:


// src/store/index.js
import Vue from 'vue';
import Vuex from 'vuex';
import counter from './modules/counter';

Vue.use(Vuex);

export default new Vuex.Store({
  modules: {
    counter
  }
});
                

Using Modules in Components

Accessing State and Getters

Access state and getters from a module in your components:


// src/components/Counter.vue



                

Using Namespaced Modules

Namespacing a module helps avoid naming conflicts and improves code organization. To namespace a module, set namespaced: true in the module definition:


// src/store/modules/counter.js
const state = {
  count: 0
};

const mutations = {
  increment(state) {
    state.count++;
  }
};

const actions = {
  increment({ commit }) {
    commit('increment');
  }
};

const getters = {
  count: state => state.count
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
};
                

Accessing Namespaced Modules

Access state, getters, mutations, and actions from a namespaced module:


// src/components/Counter.vue



                

Example Application

Here is a simple example application that demonstrates the use of Vuex modules:


// src/store/modules/counter.js
const state = {
  count: 0
};

const mutations = {
  increment(state) {
    state.count++;
  }
};

const actions = {
  increment({ commit }) {
    commit('increment');
  }
};

const getters = {
  count: state => state.count
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
};

// src/store/index.js
import Vue from 'vue';
import Vuex from 'vuex';
import counter from './modules/counter';

Vue.use(Vuex);

export default new Vuex.Store({
  modules: {
    counter
  }
});

// src/components/Counter.vue




// src/main.js
import Vue from 'vue';
import App from './App.vue';
import store from './store';

Vue.config.productionTip = false;

new Vue({
  store,
  render: h => h(App),
}).$mount('#app');
                

Best Practices

Follow these best practices when using Vuex modules:

  • Keep Modules Focused: Each module should focus on a specific area of the application state.
  • Use Namespacing: Namespace modules to avoid naming conflicts and improve code organization.
  • Document Modules: Document your modules to make them easier to understand and maintain.
  • Test Modules: Write tests for your modules to ensure they behave as expected and handle edge cases.
  • Organize Modules: Organize your modules in a logical folder structure to keep your codebase clean and maintainable.

Summary

This guide provided an overview of structuring your Vuex store using modules, including creating, registering, and using modules in your components. By understanding and utilizing modules, you can manage the state of your VueJS applications in a scalable and maintainable way.