Created
April 16, 2018 15:24
-
-
Save AlanJenkinsVS/9d940863bf842b112897e8465029c1f6 to your computer and use it in GitHub Desktop.
3. Module Registration - Vue JS Optimisations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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