Skip to content

Instantly share code, notes, and snippets.

@AlanJenkinsVS
Created April 16, 2018 15:24
Show Gist options
  • Save AlanJenkinsVS/9d940863bf842b112897e8465029c1f6 to your computer and use it in GitHub Desktop.
Save AlanJenkinsVS/9d940863bf842b112897e8465029c1f6 to your computer and use it in GitHub Desktop.
3. Module Registration - Vue JS Optimisations
// Typical module registration attaches all modules immediately to the Vuex store
import auth from './modules/auth';
import posts from './modules/posts';
import comments from './modules/comments';
// ...
export default new Vuex.Store({
modules: {
auth,
posts,
comments,
// ...
}
})
// Becomes
import modules from './modules';
export default new Vuex.Store({
modules
});
// Import
import camelCase from 'lodash/camelCase';
requireModule.keys().forEach(fileName => {
// Don't register this file as a Vuex module
if(fileName === './index.js') return;
const moduleName = camelCase(
fileName.replace(/(\.\/|\.js)/g, '')
)
modules[moduleName] = {
namespaced: true,
...requireModule(fileName),
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment