vuex subscribe to individual mutation vuex subscribe to individual mutation vue.js vue.js

vuex subscribe to individual mutation


How about wrapping the method you have somewhere in Vue prototype?

So instead of having:

this.$store.subscribe((mutation, state) => {   if(mutation === 'someMutation'){       doSomething()   }})

You would have something like:

Vue.prototype.subscribeMutation = function(someMutation, someFunction) {       this.$store.subscribe((mutation, state) => {       if(mutation === someMutation){           someFunction(state)       }    })}

I haven't tested the code, but you should be able to get the working result easily.


From vuex docs:

The handler is called after every mutation and receives the mutationdescriptor and post-mutation state as arguments.

It will react to all mutations, So you only can implement with your own conditional.


Try something like this:

this.$store.subscribe(mutation => {                if (mutation.type === 'setData') {//  do something here                }            });