vuejs / vuex

🗃️ Centralized State Management for Vue.js.
https://vuex.vuejs.org
MIT License
28.42k stars 9.58k forks source link

[Feature Request] mapModule #1579

Open yoyoys opened 5 years ago

yoyoys commented 5 years ago

What problem does this feature solve?

This made namespaced module can be use like simple vuex store.

What does the proposed API look like?

store/modules/myModule.ts


export const myModule = {
  state: {
    count: 0
  },
  mutations: {
    add(state, payload) {
      state.count = state.count   payload
    }
  }
}

lib/someLib.ts

import store from '@/store'

// the `myModule` is module path
const myModule = mapModule<IMyModule>(store, 'myModule')

console.log(myModule.state.count) // 0
myModule.commit('add', 2)
console.log(myModule.state.count) // 2

// for reuse or singleton?
export myModule
Euregan commented 5 years ago

I agree it would be very useful Another example of a use case would be when creating a swarm of simple Vue apps, each with their own stores, each in their own git repository. When developing a single app, a simple store could be instantiated, but when building the whole website, they could be aggregated as modules in a "master" store. It can't currently be done, since it has to either use namespaced modules to avoid naming collisions, and thus can't be used straightforwardly in the small apps, or avoid namespacing and force every application to avoid naming collision on its own.