vuex
vuex copied to clipboard
Add a subscribeModule instance method
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)
}
})
Relates to #1193.