vuex icon indicating copy to clipboard operation
vuex copied to clipboard

Add a subscribeModule instance method

Open ianwalter opened this issue 6 years ago • 1 comments

What problem does this feature solve?

Vuex doesn't provide a way for plugins to be notified when modules are added to the store dynamically through registerModule. This means that there can be changes in the state tree that are impossible for plugins to know about through the existing subscription methods like subscribe and subscribeAction. A common use case would be a plugin that saves the Vuex state to local storage. When a module is added through registerModule, the local storage plugin will not be able to store it's initial state to local storage.

What does the proposed API look like?

subscribeModule(handler: Function)

Subscribe to registerModule/unregisterModule events. The handler is called with a module object and a namespace when the module is registered. The module object is null when unregistered:

store.subscribeModule((mod, namespace) => {
  if (mod) {
    console.log(mod)
  } else {
    console.log(namespace)
  }
})

ianwalter avatar Feb 07 '19 17:02 ianwalter

Relates to #1193.

kiaking avatar Apr 23 '20 06:04 kiaking