Codementor Events

Vuex - the enterprise setup

Published Aug 10, 2018Last updated Feb 06, 2019
Vuex - the enterprise setup

Vuex is the official state management library for Vue.js, with a really great documentation. In my opinion, it is much nicer to use than Redux as it requires less boilerplate code and supports async actions without any additional libraries. Moreover, since it is being developed by the core Vue.js team, it integrates with Vue extremely well. Unfortunately, sometimes it still might be a bottleneck for some to get the setup right. Vue.js core team member Chris Fritz has created a great boilerplate for Vue which you can find at https://github.com/chrisvfritz/vue-enterprise-boilerplate. It has a great Vuex setup which automatically registers modules for you based on the files you created in the 'modules' folder. The folder structure looks roughly like that:

1 - folder structure.png

However, you have to have state, getters, actions, and mutations all in one file. I myself prefer to have them in separate files as there is no need to scroll through all the code to find a specific thing, especially since Vuex modules can sometimes get quite big. Therefore, we will alter the code from the boilerplate so that we can have our modules in respective folders instead.

2 - folder structure full.png

Enough of the explanation, lets get started with the business. Create a new project using vue-cli 3. You can find all the instructions at https://cli.vuejs.org/. When you have the boilerplate setup, install Vuex and Lodash by running 'npm install vuex lodash - save' in your terminal. We will need a 'camelCase' function from lodash for our modules. Now create the folder and file structure just like in the image above.

Let's start from the store.js file.

3 - store.js.png

You can see that we are importing at the top Vue and Vuex, as of course they are required. We also import modules from '/modules/index.js'. Later we initialised the store as well as loop through all the modules. If the module has an action called 'init' then it will be initialised. This is really useful when a certain module should be initialised at the start of the app. Later of course we export the store which is then usually imported in 'main.js' file and added to the Vue instance.

Now is the time to modify index.js file which is in '/store/modules' directory.

4 - index.js.png

We need to import 'camelCase' function from lodash. Then we use 'require.context' to require our modules. As the third parameter we are passing a regex which will filter out the index.js file as well as files which has 'actions', 'mutations', and 'getters' in their name. These will be imported in the 'state' file, for instance 'auth.js' and then exported. For instance, that's how the 'auth.js' file in 'src/store/modules/auth/auth.js' could look like at the start.

5 - auth.js.png

Now the only thing left to do is to loop through all of our modules and build one object with all of them. We have to exclude any files that have 'unit' in their name as these are only for the tests and not for the development or production. After that, we add a new property to the 'modules' object which will have the name of the 'state' file, e.g. 'auth' or 'users'. We also use the 'camelCase' function to make sure that the property names will be consistent. As the result of 'requireModule' is an array of filenames, we access the appropriate state file and spread the object which it exported.

That's how you can setup your project to be able to keep your state, getters, actions and mutations separately and well organised. You can find a repo with code here. In part 2 of this article you will see how to create a script to generate Vuex modules automatically with one line command in the terminal. You can find it here.

Other readings:

Building a list keyboard control component with Vue.js and scoped slots

Intro to the React Context API

Modern JavaScript features which you should be using every day for better development and what problems do they solve.

How to create a staggered animation for paginated list in Vue.js

Discover and read more posts from Thomas Findlay
get started
post comments3Replies
Robert
5 months ago

Vuex is a state bloxstrap download management pattern + library for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.

Robert
5 months ago

Vuex is a state <a href=“https://bloxstrap.dev/”>bloxstrap download</a> management pattern + library for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.

bauer mckay
a year ago

The last task at hand is to create a single object by iteratively going through each of our modules. ‘Unit’ files must be excluded from our file list since they are intended only for testing purposes, not for development or production. And, geometry dash lite is a true test of your reflexe