Ali
Ali

Reputation: 267077

How to watch for vuex mutations / actions from a component?

I need to know whenever stuff happens across the page (e.g animations starting / finishing, requests firing / failing), etc and have a component react to it.

I can watch a getter easily by importing it thru mapGetters and watching it - can I do the same to e.g watch for actions / mutations firing? E.g if requestFailed event happens (and is fired via an action / commit), I can have my component (view) react to it?

Upvotes: 0

Views: 217

Answers (1)

Anis
Anis

Reputation: 1220

you can add a plugin.js file in store directory

in plugin.js

export function plugin(store) {
    //subscribe to mutations
    store.subscribe((mutations, state) => {

    })
    //subscribe to action
    store.subscribeAction({
        before(actions, state) {

        },
        after(actions, state) {

        },
    })
}

and then you can use your plugin in index.js as

//import plugins
import { plugin } from './plugins'

const store = new Vuex.Store({
  namespaced: true,
  plugins: [plugin],
  modules: {
  }
})

Upvotes: 2

Related Questions