Declare state like props, make state inject to vuex dynamically when component created.
npm i vuex-declaration
import vuecDeclaration from 'vuex-declaration'
import Vue from 'vue'
Vue.use( vuexDeclaration );
Vue.component( 'self-component', {
data () { ... },
props : [ ... ],
created () { ... },
// Declare states
state : [ 'state-a', 'state-b' ],
} )
export default {
// Declare state with module.
state : {
'state-a' : {
module : 'module-a',
}
}
}
Append states to existing modules.
import { setStoreModule } from 'vuex-declaration'
setStoreModule( this.$store, [ 'state-a', 'state-b' ] )
Unreginster all modules from $store and then reset these.
import { resetStoreModule } from 'vuex-declaration'
resetStoreModule( this.$store, [ 'state-a', 'state-b' ] )