How to subscribe to a store module

I had the same problem and came across with this question, according to this link: https://forum.vuejs.org/t/vuex-subscribe-and-modules/36049 Subscription is always global.

My work around is to check the mutation in each subscription:

Suppose you have a module 'yourmodule' for subscription, you can do:

store.subscribe((mutation, state) => {
  if (mutation.type.startsWith("yourmodule")){
    //do your stuff here
  }
})

The mutation.type will give the actual mutation name, such as 'yourmodule/yourcommit', I use startswith() method to check because none of my modules starts the same, but you can use more accurate check for your own project.


You may try that:

store.subscribe((mutation, state) => {
  const pieces = mutation.type.split('/')
  if (pieces.length > 1) {
    const module = pieces[0]
  }
})

Tags:

Vue.Js

Vuex