I have a vuex store and i'd like to apply code splitting on it.
Following this tutorial I tried this:
import Vuex from 'vuex'
import Vue from 'vue'
import createLogger from 'vuex/dist/logger'
Vue.use(Vuex)
const debug = process.env.NODE_ENV !== 'production'
const store = new Vuex.Store({
strict: debug,
plugins: debug ? [createLogger] : [],
state: {
loading: false
},
mutations: {
toggleLoading: (state) => {
state.loading = !state.loading
}
},
getters: {
loading: state => state.loading
},
actions: {
toggleLoading: ({commit}) => {
commit('toggleLoading')
}
}
})
import('./modules/userModule').then(userModule => {
store.registerModule('user', userModule)
})
import('./modules/tenantsModule').then(tenantsModule => {
store.registerModule('tenants', tenantsModule)
})
import('./modules/updatesModule').then(updatesModule => {
store.registerModule('updates', updatesModule)
})
export default store
But the application fails :
webpack-internal:///24:739 [vuex] unknown getter: user
What am i doing wrong?
If are using export default in your modules you need to access it through dot notation (something I read here under 4.2). So something like the below worked for me:
import('./modules/userModule').then(userModule => {
store.registerModule('user', userModule.default)
})
However, I had some issues with this approach. I often use the store on an initial page load and by then the module hasn't been loaded yet. So I register the part of the store that I need initially directly when declaring the store, like so:
import userModule from './modules/userModule'
Vue.use(Vuex);
export default new Vuex.Store({
modules: {userModule}
})
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With